C++ Program to Swap Two Numbers

In this program, we will learn how to swap two numbers using C++ Programming language.

This below program asks the user to enter two numbers, then it swap those two numbers using following approaches:

  1. Using a Temporary Variable
  2. Without Using a Temporary Variable

So, without further ado, let’s begin this tutorial.

C++ Program to Swap Two Numbers Using a Temporary Variable

// C++ Program to Swap Two Numbers
#include <iostream>
using namespace std;

int main(){
    int a, b, temp;
    
    // Asking for input
    cout << "Enter the first number: ";
    cin >> a;
    cout << "Enter the second number: ";
    cin >> b;
    
    cout << "Before Swapping: " << endl;
    cout << "First Number: " << a << endl;
    cout << "Second Number: " << b << endl;
    
    // Swapping using temporary variable
    temp = a;
    a = b;
    b = temp;
    
    // Displaying output
    cout << "After Swapping: " << endl;
    cout << "First Number: " << a << endl;
    cout << "Second Number: " << b << endl;
    
    return 0;
}

Output

Enter the first number: 20
Enter the second number: 30
Before Swapping: 
First Number: 20
Second Number: 30
After Swapping: 
First Number: 30
Second Number: 20

How Does This Program Work ?

    int a, b, temp;

In this program, we have declared three integer data type variables named a, b and temp.

    // Asking for input
    cout << "Enter the first number: ";
    cin >> a;
    cout << "Enter the second number: ";
    cin >> b;

The user is asked to enter two numbers. The first number gets stored in the ‘a‘ named variable whereas the second number gets stored in the ‘b‘ named variable.

    // Swapping using temporary variable
    temp = a;
    a = b;
    b = temp;

We assign the value of ‘a’ to a temporary variable temp. Then, we assign the value of ‘b‘ to ‘a‘. After that, the content of temp is assigned to ‘b‘.

By this way, the two numbers get swapped using a temporary variable.

Suppose a = 20, and b = 30. Then, we have:
temp = a => temp = 20
a = b => a = 30
b = temp => b = 20

Thus, we get a = 30 and b = 20 after swapping.

C++ Program to Swap Two Numbers Without Using a Temporary Variable

// C++ Program to Swap Two Numbers Without Using a Temporary Variable
#include <iostream>
using namespace std;

int main(){
    int a, b;
    
    // Asking for input
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    
    cout << "Before Swapping: " << endl;
    cout << "First Number: " << a << endl;
    cout << "Second Number: " << b << endl;
    
    // Swapping without using a temporary variable
    a = a + b;
    b = a - b;
    a = a - b;
    
    // Displaying output
    cout << "After Swapping: " << endl;
    cout << "First Number: " << a << endl;
    cout << "Second Number: " << b << endl;
    
    return 0;
}

Output

Enter first number: 17
Enter second number: 22
Before Swapping: 
First Number: 17
Second Number: 22
After Swapping: 
First Number: 22
Second Number: 17

Conclusion

I hope after going through this post, you understand how to swap two numbers using C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to solve your doubts.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *