7+ Java Programs to Swap Numbers

This post contains a total of 7+ Java Program Examples with Source Code to Swap Numbers. All these programs to Swap Numbers are made using Java.

You can use the source code of these examples with credits to the original owner.

Related Posts

Java Programs to Swap Numbers

1. By Sujan Ahmed

Made by Sujan Ahmed. Program to Swap Two Numbers. Source

Before swapping : 
a = 5
b = 10
After swapping : 
a = 10
b = 5
public class swapping{
    
    int a = 5;
    int b = 10;
    
}

class myClass{
    public static void main(String[] arg){
        swapping num = new swapping();
        System.out.println("Before swapping : \na = " + num.a + "\nb = " + num.b);
        swap(num);
        System.out.println("After swapping : \na = " + num.a + "\nb = " + num.b);
    }
    
    
    
    
    public static void swap(swapping p){
         int temp = p.a;
         p.a = p.b;
         p.b = temp;
        
    }
}

2. By Real Gutch

Made by Real Gutch. Small Java Program to Swap Numbers. Source

i = 20, j = 10
public class Program
{
    public static void main(String[] args) {
        int i = 10;
        int j = 20;
        j = i+j-(i=j);
        System.out.println("i = "+i+", j = "+j);
    }
}

3. By Mahesh Chavda

Made by Mahesh Chavda. Simple two Numbers swapping program. Source

Enter numbers you want to swap :- 1 2
-----------------------------------
Before Swapping
-----------------------------------
x = 1
y = 2
-----------------------------------
After Swapping
-----------------------------------
x = 2
y = 1
import java.util.Scanner;
class swap { 
 public static void main(String args[]) 
     { 
         Scanner sc = new Scanner(System.in);
         
         int x, y, temp; 

         System.out.println("Enter numbers you want to swap :-");
         System.out.println("-----------------------------------");
         x = sc.nextInt();
         y = sc.nextInt();
         
          
         
         System.out.println("Before Swapping\n-----------------------------------\nx = "+x+"\ny = "+y);
         System.out.println("-----------------------------------");
         temp = x; 
         x = y; 
         y = temp;
         
         System.out.println("After Swapping\n-----------------------------------\nx = "+x+"\ny = "+y); 
     } 
 }

4. By Piyush Kumar Singh

Made by Piyush Kumar Singh. Program to Swap numbers without using third Variable. Source

a=6 b=5
import java.util.Scanner;
public class SwapNumber {

	public static void main(String[] args) 
	{
		Scanner sc=new Scanner(System.in);
	int a=sc.nextInt();
	int b=sc.nextInt();
	a=a+b;
	b=a-b;
	a=a-b;
	System.out.println("a="+ a + "b=" +b);

	}

}

5. By Subhash Dhandhukiya

Made by Subhash Dhandhukiya. Program to Swap numbers using third variable. Source

Enter the value of x:4
Enter the value of y:5
After x=5
After y=4
import java.util.Scanner;
class Test
{
    public static void main(String []args)
    {
        int x,y,temp;
        Scanner obj=new Scanner(System.in);
        
        System.out.print("Enter the value of x:");
        x=obj.nextInt();
        System.out.println(x);
        
        System.out.print("Enter the value of y:");
        y=obj.nextInt();
        System.out.println(y);
        
        temp=x;
        x=y;
        y=temp;
        
        System.out.println("After x="+x);
        System.out.println("After y="+y);
    }
}

6. By RANJITHKUMAR.M

Made by RANJITHKUMAR.M. Source

5
6
6
5
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) 
    {
    Scanner s=new Scanner(System.in);
    
        int a=s.nextInt();
        int b=s.nextInt();
        System.out.println(a);
        System.out.println(b);
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println(a);
        System.out.println(b);
    }
}

7. By Sagar Chhabra

Made by Sagar Chhabra. Java number swap program using only two variable. Source

Enter two numbers :6 7
Previously, x =  6 and y = 7
After swapping ,value of x = 7 and value of y= 6
import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        
Scanner a=new Scanner(System.in);
System.out.println("Enter two numbers :");
int x=a.nextInt();
int y=a.nextInt();

System.out.println("Previously, x =  "+x+" and y = "+y);

x=x+y;
y=x-y;
x=x-y;

System.out.println("After swapping ,value of x = "+x+" and value of y= "+y);

System.out.println("Thank you guys for having  look at this number swap. I hope you like it ");
    }
}

8. By Reza Safari

Made by Reza Safari. Source

Enter Value in a :7
Enter Value in b :8
Values in a:8
Values in b:7
import java.util.Scanner;
 class Swapnumber { 
     public static void main(String[] args) { 
       int a, b, c; 
       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();
       c=a; 
       a=b; 
       b=c; 
       System.out.println("Values in a:" +a);
       System.out.println("Values in b:" +b); 
     } 
 }