Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Program 05: Swap two integer variables without using third variable

Posted on 03/21/2025 By admin

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a Java program to swap values of two integer variables without using third variable.

Problem Statement:

Write a java program to swap values of two integer variables without using third variable.

Solution:

It is very commonly asked interview program. In Fact it is less program more mathematics.

  1. Let’s assume there are two variables with some values:

             a =10

             b=20

     2. Now add a and b and store in a:

             a = a+b => 10+20 => 30

             b= 20

     3. Now subtract b from a and store in b:

            a= 30

            b = a – b => 30-20 => 10

     4. Now subtract b from a and store in a:

            a= a – b = > 30-10 = > 20

            b= 10

     5. Notice here that both variables a and b have interchange their values. 

Let’s convert above logic into a Java Program.

Java Program:
import java.util.Scanner;

public class SwappingTwoNumbers {

        public static void main(String[] args) {
                
                // taking input from user
                Scanner in = new Scanner(System.in);
                System.out.println("Enter the 1st number: ");
                int x = in.nextInt();
                System.out.println("Enter the 2nd number: ");
                int y = in.nextInt();
                 
                // printing values before swap
                System.out.println("Before Swap: ");
                System.out.println("Value of x: "+x);
                System.out.println("Value of y: "+y);
                
                // swapping without using third variable
                x = x+y;
                y = x-y;
                x = x-y;
                 
                // printing values after swap
                System.out.println("After Swap: ");
                System.out.println("Value of x: "+x);
                System.out.println("Value of y: "+y);
        }
}

Output:

[java]
Enter the 1st number:
21
Enter the 2nd number:
33
Before Swap:
Value of x: 21
Value of y: 33
After Swap:
Value of x: 33
Value of y: 21
[/java]

#HappyCoding

Uncategorized

Post navigation

Previous Post: List Of Frequently Asked Manual Testing Interview Questions( 200+)….
Next Post: TestNG Tutorials 24: Annotations In TestNG – A Quick Guide

Related Posts

Java For Selenium Uncategorized
June 25, 2018 – Make Selenium Easy Uncategorized
InnerClassTestNg – Make Selenium Easy Uncategorized
staleExc – Make Selenium Easy Uncategorized
scrollTo500 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark