Write A Java Program To Swap Two Numbers Without Using The Third Variable.
import java.util.Scanner;
class swap TwoNumberWithoutThirdVariable
{
public static void main(String args[ ])
{
int x, y;
System.out .println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt( );
y = in.nextInt( );
System.out.println("Before swapping\ nx = "+x+"\ ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping Without Third Variable\nx = "+x+"\ny = "+y);
}
}
Output:
Enter x and y
45
98
Before Swapping
x =45
y = 98
After Swapping Without a Third Variable
x = 98
y = 45
0 comments 10:
Post a Comment