Write a program to swap two numbers

Posted in

Write a program to swap two numbers
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Problem Statement

    We have given two numbers num_1 and num_2, which are the two integer number variables entered by the user. We need to write a program that swaps the value of num_1 with num_2

    For example

    Input

    num_1 = 20
    num_2 = 40

    Output

    num_1 = 40
    num_2 = 20
    

    Approach 1 (Using a temporary variable)

    In our first approach, we will take the help of a temporary variable temp to swap the values of num_1 and num_2 . We will first store the value of num_1 in temp , then assign the value of num_2 to num_1 and at last assign, the value of temp to num_2 . This will make sure that the values of num_1 and num_2 are swapped.

    C Program to swap two numbers (using temp variable)

    #include <stdio.h>
    int main()
    {	
    	int num_1,num_2,temp;
    	
    	//input the first number
    	printf("Enter the value of num_1: ");
    	scanf("%d", &num_1);
    	
    	//input the second number
    	printf("Enter the value of num_2: ");
    	scanf("%d", &num_2);
    	
    	
    	//swap the numbers 
    	temp =num_1;
    	num_1= num_2;
    	num_2 = temp;
    	
    	printf("After Swapping\n");
    	
    	printf("The value of num_1 is: %d \n", num_1);
    	printf("The value of num_2 is: %d \n", num_2);
    	
    	return 0;
    }

    Output

    Enter the value of num_1: 12
    Enter the value of num_2: 34
    After Swapping
    The value of num_1 is: 34
    The value of num_2 is: 12

    C++ Program to swap two numbers (using temp variable)

    #include<iostream>
    using namespace std;
    int main()
    {
    	int num_1,num_2,temp;
    	
    	//input the first number
    	cout<<"Enter the value of num_1: "; cin>>num_1;
    	
    	//input the second number
    	cout<<"Enter the value of num_2: "; cin>>num_2;
    	
    	
    	//swap the numbers 
    	temp =num_1;
    	num_1= num_2;
    	num_2 = temp;
    	
    	cout<<"After Swapping\n";
    	
    	cout<<"The value of num_1 is: "<< num_1<<endl;
    	cout<<"The value of num_2 is: "<<num_2<<endl;
    	
    	return 0;
    }

    Output:

    Enter the value of num_1: 34
    Enter the value of num_2: 53
    After Swapping
    The value of num_1 is: 53
    The value of num_2 is: 34

    Python Program to Swap two numbers(using temp variable)

    # input the first number
    num_1= int(input("Enter the value of num_1: "))
    
    # input the second number
    num_2= int(input("Enter the value of num_2: "))
    
    
    # swap the numbers
    temp = num_1
    num_1 = num_2
    num_2 = temp
    
    print("After swapping")
    print(f"The value of num_1 is: {num_1}")
    print(f"The value of num_2 is: {num_2}")

    Output:

    Enter the value of num_1: 34
    Enter the value of num_2: 52
    After swapping
    The value of num_1 is: 52
    The value of num_2 is: 34

    Approach 2 (Without using a temporary variable)

    Most of the time, when we want to swap two values of two variables, we generally use an extra temp or temporary variable. But when both the variables are integers or numbers we can also swap their values without using a temp variable. If both the variables are numbers, we can use the help of addition and subtraction to swap their value.

    Example

    if a = 20 and b = 30
    a = a+b ;   // 20+30 = 50
    b = a - b ;  // 50- 30 = 20
    a = a - b =   // 50 - 20 = 30
    
    now a = 30 and b = 20

    C program to swap two numbers (without using temp variable)

    #include <stdio.h>
    
    int main()
    {	
    	int num_1,num_2,temp;
    	
    	//input the first number
    	printf("Enter the value of num_1: ");
    	scanf("%d", &num_1);
    	
    	//input the second number
    	printf("Enter the value of num_2: ");
    	scanf("%d", &num_2);
    	
    	
    	//swap the numbers 
    	num_1 = num_1+num_2;
    	num_2 = num_1 - num_2;
    	num_1 = num_1- num_2;
    	
    	printf("After Swapping\n");
    	
    	printf("The value of num_1 is: %d \n", num_1);
    	printf("The value of num_2 is: %d \n", num_2);
    	
    	return 0;
    }

    Output

    Enter the value of num_1: 23
    Enter the value of num_2: 43
    After Swapping
    The value of num_1 is: 43
    The value of num_2 is: 23

    C++ program to swap two numbers (without using temp variable)

    #include<iostream>
    using namespace std;
    int main()
    {
    	int num_1,num_2,temp;
    	
    	//input the first number
    	cout<<"Enter the value of num_1: "; cin>>num_1;
    	
    	//input the second number
    	cout<<"Enter the value of num_2: "; cin>>num_2;
    	
    	
    	//swap the numbers 
    	num_1 = num_1+num_2;
    	num_2 = num_1 - num_2;
    	num_1 = num_1- num_2;
    	
    	cout<<"After Swapping\n";
    	
    	cout<<"The value of num_1 is: "<< num_1<<endl;
    	cout<<"The value of num_2 is: "<<num_2<<endl;
    	
    	return 0;
    }

    Output

    Enter the value of num_1: 34
    Enter the value of num_2: 75
    After Swapping
    The value of num_1 is: 75
    The value of num_2 is: 34

    Python program to swap two numbers (without using temp variable)

    # input the first number
    num_1= int(input("Enter the value of num_1: "))
    
    # input the second number
    num_2= int(input("Enter the value of num_2: "))
    
    # swap the numbers
    num_1 = num_1+num_2
    num_2 = num_1 - num_2
    num_1 = num_1- num_2
    
    print("After swapping")
    print(f"The value of num_1 is: {num_1}")
    print(f"The value of num_2 is: {num_2}")

    Output

    Enter the value of num_1: 34
    Enter the value of num_2: 78
    After swapping
    The value of num_1 is: 78
    The value of num_2 is: 34

    Wrapping Up!

    In this programming tutorial, we discussed how to swap two numbers with and without using a third temporary variable. In the first approach, we demonstrate how can we use a third variable temp and swap the value of two variables. And in the second approach, we show how to swap the numbers without using the temporary variable. The problem statement is very simple and often asked in programming or technical round interviews to check if the programmer knows how to solve the problem without using a temporary variable.

    People are also reading:

    Leave a Comment on this Post

    0 Comments