import
java.util.Scanner;
public class
SwapElementsWithoutThirdVariableExample {
public static void main(String[]
args) {
Scanner
scan = new Scanner(System.in);
System.out.println("Enter
the value of a ");
int a =
scan.nextInt();
System.out.println("Enter
the value of b");
int b =
scan.nextInt();
System.out.println("Before
Swapping");
System.out.println("Value of
a is :" + a);
System.out.println("Value of
b is :" + b);
a
= a + b;
b
= a - b;
a
= a - b;
System.out.println("After
Swapping");
System.out.println("Value of
a is :" + a);
System.out.println("Value
of b is :" + b);
}
}
Output:
Enter
the value of a
100
Enter
the value of b
200
Before
Swapping
Value
of a is :100
Value
of b is :200
After
Swapping
Value
of a is :200
Value
of b is :100
Have you checked this Write a program to Swap two variables or number.One can find almost all the methods of swapping of two numbers.
ReplyDelete