Write a program swapping two numbers without using third variable in java ?

Swap Two numbers without using third variable:
Write a program to exchange or swap two numbers.We must not use any temporary or third variable to swap.

import java.util.Scanner;

/**
*
* @author Ganta Suresh Babu
*/
public class swapingnumbers {
public static void main(String args[]){
int a1,a2;
System.out.print(“enter the a1 value : “);
Scanner j=new Scanner(System.in);
a1=j.nextInt();
System.out.print(“enter the a2 value ; “);
a2=j.nextInt();
a1=a1+a2;
a2=a1-a2;
a1=a1-a2;
System.out.println(“a1 value After swapping a1 value: “+a1);
System.out.println(“a2 value After swapping a1 value: “+a2);

}
}

Output:

Enter the a1 value : 8
Enter the a2 value ; 5
a1 value After swapping : 5    
 a2 value After swapping: 8

Related Programs Below:
Write a program the given number is even or odd in java?
Write a program for the given number is Fibonacci or not in java?
Prime numbers in Java?
Palindrome number Program in java?
Armstrong Number or not in Java?
Write a program for the given number is strong Number or Not  in java?
Write a program swapping two numbers without using third variable in java ?
Swapping of two numbers using third variable in java?

Leave a Reply

Your email address will not be published.