Saturday, November 15, 2014

Filled Under: , , , , , ,

Program to swap two values without third variable in java.

Share
Swapping without third variable


Code Box
---------------------------------------------

/*program to swap two values without using third variable */

import java.io.*;

class swap
{
public static void main(String x[]) throws IOException
{
int a,b;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\nEnter two numbers :");
System.out.print("\nEnter first number a= ");
a=Integer.parseInt(obj.readLine());


System.out.print("\nEnter second number b= ");
b=Integer.parseInt(obj.readLine());


System.out.print("\nValues before swapping a= "+a+" & b= "+b);

a=a+b;   //swapping
b=a-b;
a=a-b;

System.out.print("\nValues after swapping a= "+a+" & b= "+b);

}
}


0 comments:

Post a Comment