public class SwappingNum {
public static void main(String[] args) {
int a=6;
int b=10;
System.out.print("Before swapping : A = "+a+" & B = "+b+"\n");
//Swapping using third variable
int temp;
temp=a;
a=b;
b=temp;
System.out.print("After swapping : A = "+a+" & B = "+b+"\n");
////Swapping without third variable
a=a+b;
b=a-b;
a=a-b;
System.out.print("After swapping : A = "+a+" & B = "+b);
}
}
//Before swapping : A = 6 & B = 10
//After swapping : A = 10 & B = 6
//After swapping : A = 6 & B = 10
public static void main(String[] args) {
int a=6;
int b=10;
System.out.print("Before swapping : A = "+a+" & B = "+b+"\n");
//Swapping using third variable
int temp;
temp=a;
a=b;
b=temp;
System.out.print("After swapping : A = "+a+" & B = "+b+"\n");
////Swapping without third variable
a=a+b;
b=a-b;
a=a-b;
System.out.print("After swapping : A = "+a+" & B = "+b);
}
}
//Before swapping : A = 6 & B = 10
//After swapping : A = 10 & B = 6
//After swapping : A = 6 & B = 10
No comments:
Post a Comment