Write a Java Program to Swap two numbers without using third variable

By | 19.01.2017

Java Program to Swap two numbers without using third variable


SOURCE CODE : :

import java.util.Scanner;
class Swapnumber 
{
 public static void main(String[] args) 
 {
 int a, b;
 Scanner s=new Scanner(System.in);
 System.out.println("Enter Value in a :");
 a=s.nextInt();
 System.out.println("Enter Value in b :");
 b=s.nextInt();
 a=a+b;
 b=a-b;
 a=a-b;
 System.out.println("Values in a:" +a);
 System.out.println("Values in b:" +b);
 }
}

 

OUTPUT ::

 

Enter Value in a :
5
Enter Value in b :
2
Value in a : 2
Value in b : 5

 

3.5 2 votes
Article Rating
Category: Basic Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments