Swapping Two Numbers Without Third Variable Method 2 Program in Java and Python
Java and Python program to swap two numbers without using a third variable by using addition and subtraction.
Question:
Write a program to input two integers and swap their values without using any third variable.
In this method, use simple mathematical operations, namely addition and subtraction. The program must display the values of both variables before and after swapping.
Example
Algorithm:
Step 1: Start.
Step 2: Create a Scanner object to accept input from the user.
Step 3: Accept the first number and store it in a.
Step 4: Accept the second number and store it in b.
Step 5: Display the values of a and b before swapping.
Step 6: Store the sum of both values in a by writing a = a + b.
Step 7: Store the original value of a in b by writing b = a - b.
Step 8: Store the original value of b in a by writing a = a - b.
Step 9: Display the values of a and b after swapping.
Step 10: Stop.
Explanation:
This program swaps two numbers without declaring a temporary third variable. The method used here is based on addition and subtraction. Usually, while swapping two values, a third variable is used to temporarily hold one value so that it is not lost. In this method, the sum of both values is used as the temporary information, but it is stored inside one of the existing variables.
Suppose the original values are a = 25 and b = 13. The first operation is a = a + b, so a becomes 38. This value contains both original numbers combined. The variable b still contains 13. The second operation is b = a - b. Since a is now 38 and b is still 13, the result is 25. Thus b now receives the original value of a.
The third operation is a = a - b. At this point, a is the sum 38, and b has become 25. Subtracting gives 13, which is the original value of b. Therefore a now contains 13, and b contains 25. The two values have been exchanged without using any extra variable.
This method is easy for school-level programs because it uses only arithmetic operators. It should be used with ordinary integer values where the sum does not exceed the storage range of the integer type. The program prints the numbers before applying the three statements and then prints them again after the swapping statements, making the change clear in the output.
Java Program:
/**
* The class Swapping_Method2 takes two numbers as input
* and swaps their values without using a third variable.
* This method uses addition and subtraction.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Swapping_Method2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 1st no: ");
int a = sc.nextInt();
System.out.print("Enter the 2nd no: ");
int b = sc.nextInt();
System.out.println("-------------------------------");
System.out.println("The numbers before swapping are");
System.out.println("a = " + a);
System.out.println("b = " + b);
// Beginning of swapping using addition and subtraction.
a = a + b;
b = a - b;
a = a - b;
// End of swapping.
System.out.println("-------------------------------");
System.out.println("The numbers after swapping are");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}Equivalent Python Program:
# Program to swap two numbers without using a third variable.
# This method uses addition and subtraction.
a = int(input("Enter the 1st no: "))
b = int(input("Enter the 2nd no: "))
print("-------------------------------")
print("The numbers before swapping are")
print("a =", a)
print("b =", b)
# Beginning of swapping using addition and subtraction.
a = a + b
b = a - b
a = a - b
# End of swapping.
print("-------------------------------")
print("The numbers after swapping are")
print("a =", a)
print("b =", b)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.