Q. Write an algorithm and program to swap two numbers without using temporary variable.

Solution : We can swap two numbers without using temporary variable. By using + and - operator we can swap two number.

Swapping two number :- Swap two numbers means exchange the values of two variables with each other.
For example:
Before Swapping : num1 = 10 and num2 = 20.
After Swapping : num1 = 20 and num2 = 10.

Logic :- Suppose we are given 2 numbers num1 = 10 and num2 = 20 and we have to swap these 2 numbers without using third variable. Then we have to perform below operations to swap the numbers.

1. Firstly we will add both the number and will assign to the first number.
num1 = num1 + num2
num1 = 10 + 20

2. Secondly we will subtract second number from the first number (updated value after step 1)
num2 = num1 - num2
num2 = 10 + 20 - 20
num2 = 10

3. Third step will be subtracting second number (updated value after step 2) from the first number (updated value from step 1)
num1 = num1 - num2
num1 = 10 + 20 - 10
num1 = 20

After the above 3 steps we will get our required result i.e we have swapped 2 numbers without using third variable.

Algorithm to swap two numbers without using third variable

//Algorithm To Swap Two Numbers Without Using Temporary Variable
START
Step 1 ->  Take two integer as input num1 and num2.
Step 2 ->  Print number before swapping
Step 3 ->  num1 = num1 + num2;
Step 4 ->  num2 = num1 - num2;
Step 5 ->  num1 = num1 - num2;
Step 6 ->  Print numbers after swapping
STOP


Program to swap two numbers without using temporary variable

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Swap Two Numbers Without Temporary Variable.
    #include <stdio.h>
    int main()
    {
    int num1,num2;
    printf("Enter Two Numbers :\n");
    scanf("%d %d",&num1,&num2);
    printf("Number before swapping is %d and %d \n",num1,num2);
    num1=num1+num2;
    num2=num1-num2;
    num1=num1-num2;
    printf("Number after swapping is %d and %d \n",num1,num2);
    return 0;
    }
    
    //C++ Program To Swap Two Numbers Without Temporary Variable.
    #include <iostream>
    using namespace std;
    int main()
    {
    int num1,num2;
    cout<<"Enter Two Numbers :\n";
    cin>>num1>>num2;
    cout<<"Number before swapping is "<<num1<<" and "<<num2<<"\n";
    num1=num1+num2;
    num2=num1-num2;
    num1=num1-num2;
    cout<<"Number after swapping is "<<num1<<" and "<<num2;
    return 0;
    }
    
    //Java Program To Swap Two Numbers Without Temporary Variable.
    import java.util.Scanner;
    public class LFC
    {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter Two Numbers :");
    int num1 = scan.nextInt();
    int num2 = scan.nextInt();
    System.out.println("Number before swapping is "+num1+" and "+num2);
    num1=num1+num2;
    num2=num1-num2;
    num1=num1-num2;
    System.out.println("Number after swapping is "+num1+" and "+num2);
    }
    }
    
    //Python Program To Swap Two Numbers Without Temporary Variable.
    print('Enter Two Numbers :')
    num1 = int(input())
    num2 = int(input()) 
    print("Number before swapping is ",num1," and ",num2)
    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;
    print("Number after swapping is ",num1," and ",num2)
    
    //C# Program To Swap Two Numbers Without Temporary Variable.
    using System;
    class LFC {
    static void Main() {
    int num1=10,num2=15;
    Console.WriteLine("Number before swapping is "+num1+" and "+num2);
    num1=num1+num2;
    num2=num1-num2;
    num1=num1-num2;
    Console.WriteLine("Number After swapping is "+num1+" and "+num2);
    }
    } 
    //PHP Program To Swap Two Numbers Without Temporary Variable.
    <?php
    $num1=10;
    $num2=20;
    echo "Number before swapping is $num1 and $num2 \n";
    $num1=$num1+$num2;
    $num2=$num1-$num2;
    $num1=$num1-$num2;
    echo "Number after swapping is $num1 and $num2";
    ?>
    //C Program To Swap Two Numbers Without Temporary Variable.
    #include <stdio.h>
    int main()
    {
    int num1,num2;
    printf("Enter two numers : \n");
    scanf("%d %d",&num1,&num2);
    printf("Number before swapping is %d and %d \n",num1,num2);
    num1=num1+num2;
    num2=num1-num2;
    num1=num1-num2;
    printf("Number after swapping is %d and %d \n",num1,num2);
    return 0;
    }
    

    Output

    Enter Two Numbers :
    10 15
    Number before swapping is 10 and 15
    Number after swapping is 15 and 10
    




    Recommended Programs

       Find all pairs of elements in an integer
       Count occurrence of a given character in a string