Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java Program to Swap Two Numbers

by suresh

Write a Java Program to Swap Two Numbers using temporary variable, and also without using temporary or third variable. For this purpose we are going to use Arithmetic Operators, and Bitwise Operators.

Java Program to Swap Two Numbers using Temp Variable

This program allows the user to enter two integer values. Using the third variable, this program will Swap those two numbers.

// Java Program to Swap Two Numbers
import java.util.Scanner;

public class SwapNumbers1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int a, b, temp;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the First value : ");
		a = sc.nextInt();	
		
		System.out.print(" Please Enter the Second value : ");
		b = sc.nextInt();	
		
		System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);
		
		temp = a;
		a = b;
		b = temp;
		
		System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
	}
}

OUTPUT

Java Program to Swap Two Numbers 1

ANALYSIS
In the above example, we assigned a = 10 and b = 25

Temp = a – it means assigning a value to Temp variable
Temp = 10

a = b – assigning b value to the variable a
a = 25

b = Temp – assigning Temp value to b
b = 6

Java Program to Swap Two Numbers without Using Temp Variable

In this program, Instead of using the temp, or third variable to swap two numbers we are going to use Arithmetic Operators.

// Java Program to Swap Two Numbers
import java.util.Scanner;

public class SwapNumbers2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int a, b;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the First value : ");
		a = sc.nextInt();	
		
		System.out.print(" Please Enter the Second value : ");
		b = sc.nextInt();	
		
		System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);
		
		a = a + b;
		b = a - b;
		a = a - b;
		
		System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
	}
}

OUTPUT

Java Program to Swap Two Numbers 2

ANALYSIS
User Entered Values are a = 6 and b = 13

a = a + b
a = 6 + 13 = 19

b = a – b;
b = 19 – 13 = 6

a = a – b;
a = 19 – 6 = 13
Final Values after swapping 2 numbers: a = 13 and b = 6

NOTE: Although we can swap 2 numbers using multiplication, and division approach. But it may produce strange values if we are working with larger integer values, or if any of the integer value is 0

Java Program to Swap Two Numbers using Bitwise OR Operator

In this program, Instead of using the temp variable we are going to use Bitwise Operators to swap 2 numbers.

// Java Program to Swap Two Numbers
import java.util.Scanner;

public class SwapNumbers3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int a, b;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the First value : ");
		a = sc.nextInt();	
		
		System.out.print(" Please Enter the Second value : ");
		b = sc.nextInt();	
		
		System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);
		
		a = a ^ b;
		b = a ^ b;
		a = a ^ b;
		
		System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
	}
}

OUTPUT

Java Program to Swap Two Numbers 3

Java Program to Swap Two Numbers Using Method

This program is same as first example but we separated the logic and placed it in the separate method.

// Java Program to Swap Two Numbers
import java.util.Scanner;

public class SwapNumbers4 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int a, b;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the First value : ");
		a = sc.nextInt();	
		
		System.out.print(" Please Enter the Second value : ");
		b = sc.nextInt();	
		
		System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);
		swapping(a, b);
		
	}
	
	public static void swapping(int a, int b)
	{
		int temp;
		
		temp = a;
		a = b;
		b = temp;
		
		System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
	}
}

OUTPUT

Java Program to Swap Two Numbers 4

Placed Under: Java, Java Programs

Trending Posts

How to Swap Column Values in SQL Server

JavaScript toLocaleDateString

SSIS Web Service Task

JavaScript FromCharCode

C Program for Bubble Sort

Java String contains Method

Python MODF

MySQL DEGREES Function

JavaScript Switch Case

Insert Page Breaks in SSRS Report

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy