Swapping three numbers without have a temporary variable

Program:-

package payilagam.institution;

public class Swap3 {

	public static void main(String[] args) 
	{
    
		int no1 = 10;
		int no2 = 20;
		int no3 = 30;
	System.out.println("Before Swapping no1 - " +no1);
	System.out.println("Before Swapping no2 - " +no2);
	System.out.println("Before Swapping no3 - " +no3);
		System.out.println();
		
		no2 = no1+no2;
    	        no1 = no3-no1;
		no3 = no3-no1;
		
	System.out.println("After Swapping no1 - " +no1);
	System.out.println("After Swapping no2 - " +no2);
	System.out.println("After Swapping no3 - " +no3);
	}

}

Output Screenshot:-

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free