Problem statement: Swap four variables without using fifth variables !!
Problem solving:
Problem solving:
- public class SwapFourNumberWithoutFifthVariable {
- public static void main(String[] args) {
- int a = 10;
- int b = 20;
- int c = 30;
- int d = 40;
- a = a + b + c + d;
- b = a - (b + c + d);
- c = a - (b + c + d);
- d = a - (b + c + d);
- a = a - (b + c + d);
- System.out.println("after swap value of a: " + a);
- System.out.println("after swap value of b: " + b);
- System.out.println("after swap value of c: " + c);
- System.out.println("after swap value of d: " + d);
- }
- }
Output:
after swap value of a: 40
after swap value of b: 10
after swap value of c: 20
after swap value of d: 30
No comments:
Post a Comment