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.

Swap two numbers without using third variable algorithm

//Algorithm To Swap Two Numbers Without 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


Swap Two Numbers Without Using Temporary Variable Program

  • 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