Swapping two number in java:

What is swapping in java:

In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory.

Swapping with two numbers:

Swapping two number in C programming language means exchanging the values of two variables. Suppose you have two variable var1 & var2.

Example:

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

output:

Leave a Comment