Simple Java program to Swap values

In this post, I would like to share two simple program which swap value stored in two variables. In the first program, uses an extra variable to perform swapping and in the second program there is no additional variable used. These two program uses two variable a and b to hold the values inserted by user. After processing the value in variables get replaced by each other. That is the value in a get replace by the value in b and the value in b get replaced by the value in a. For instance if user inserts 10 and 25 into the variable a and b respectively, after process the variable a will have 25 and b will have 10. To perform this operation, normally,we need an extra variable which holds the value in variable either the value in a or in b. If we store the value in a to the additional variable, say s, then we can replace a with b's value. Now, we have variable a with the value of variable b. The variable s holds the value of a and it can be used to replace the value in b. Now the value get swapped in a and b. Check the program below
Code:


 //Program to swap values  
 import java.io.*;  
 class MySwap{  
 public static void main(String arg[])throws IOException  
 {  
 int a=0,b=0,s=0;  
 DataInputStream dip=new DataInputStream(System.in);  
 System.out.println("\n");  
 System.out.println("\t*-*PROGRAM TO SWAP VALUES IN TWO VARIABLES*-*");  
 System.out.print("\t");  
 for(int i=0;i<15;i++)  
 {System.out.print("_ _");}  
 System.out.println("\n");  
 System.out.print("Insert a number into variable a= ");  
 a=Integer.parseInt(dip.readLine());  
 System.out.print("Insert a number into variable b=");  
 b=Integer.parseInt(dip.readLine());  
 System.out.println("Befor swapping a="+a+" and b= "+b);  
 s=a;  
 a=b;  
 b=s;  
 System.out.println("After swapping a="+a+" and b= "+b);  
 }}  

Output:
In the above program uses an additional variable, s, which hold the value of the variable a. Now, we perform the above process without any additional variable other than the two variable which holds the value inserted by the user. The code is given below
Code:
 //Program to swap two values without an additional variable  
 import java.io.*;  
 class Swapstv{  
 public static void main(String arg[])throws IOException  
 {  
 int a=0,b=0;  
 int n=0,i,c,flag=0,ams;  
 DataInputStream dip=new DataInputStream(System.in);  
 System.out.println("\n");  
 System.out.println("\t*-- PROGRAM TO SWAP VALUES --*");  
 System.out.print("\t");  
 for( i=0;i<10;i++)  
 {System.out.print("_ _");}  
 System.out.println("\n");  
 System.out.println("Enter the values into variable a");  
 a=Integer.parseInt(dip.readLine());  
 System.out.println("Enter the values into variable b");  
 b=Integer.parseInt(dip.readLine());  
 System.out.print("Before swapping, ");  
 System.out.print("Value in a= "+a+" and");  
 System.out.println(" Value in b= "+b);  
 //swap proccess  
 a=a+b;  
 b=a-b;  
 a=a-b;  
 System.out.print("After swapping, ");  
 System.out.print("Value in a= "+a+" and");  
 System.out.println(" Value in b= "+b);  
 }  
 }  

Output:

No comments: