Showing posts with label engineering. Show all posts
Showing posts with label engineering. Show all posts

Saturday, November 15, 2014

, , , , , ,

Program to swap two values without third variable in java.

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);

}
}


Publisher: Brijmohan Lal Sahu - 10:20 AM
, , , , , ,

Program to swap two values using third variable in java.

Swapping using third variable


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

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

import java.io.*;

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

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);

temp=a;   //swapping
a=b;
b=temp;

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

}
}



Publisher: Brijmohan Lal Sahu - 10:14 AM