Java Program - 04 - Write a Java program to swap two numbers without using temporary variable.
//Write a Java program to swap two numbers without using temporary variable.
public class StudyViral_04 {
public static void main(String[] args) {
int x = 100;
int y = 200;
System.out.println("Output :: ");
System.out.println("Before swap:");
System.out.println("value of x :: "+x);
System.out.println("value of y :: "+y);
x = x+y; // here x = 100+200 = 300
y=x-y; // here y = 300-200 = 100
x=x-y; // here x = 300-100 = 200
System.out.println("After swap:");
System.out.println("value of x :: "+x);
System.out.println("value of y :: "+y);
}
}
Output ::
Before swap:
value of x :: 100
value of y :: 200
After swap:
value of x :: 200
value of y :: 100
0 comments:
Post a Comment