Thursday 11 February 2016

Swamping of two numbers using temporary variable

Swamping of two numbers using temporary variable:
=======================================
package files;

public class SwapDemo {

public static void main(String[] args) {

int x = 20, y = 30,temp;

System.out.println("Before Swapping\nx = " + x + "\ny = " + y);

temp = x;
x  =  y;
y  =  temp;

System.out.println("After Swapping\nx = " + x + "\ny = " + y);
}

}

OUTPUT:
=========
Before Swapping
x = 20
y = 30
After Swapping
x = 30

y = 20

No comments:

Post a Comment