C++ Program to Swap Two Numbers Using Temporary Variable

1. Introduction

Swapping two numbers is a fundamental operation in programming and computer algorithms. Often, it's among the first few programs taught to budding programmers. Swapping values has applications in sorting algorithms, mathematical computations, and many day-to-day programming scenarios. In this post, we will learn how to swap two numbers using a temporary variable in C++.

2. Program Overview

The logic behind swapping two numbers using a temporary variable is straightforward:

1. Store the value of the first number in a temporary variable.

2. Assign the value of the second number to the first number.

3. Assign the value of the temporary variable (which contains the original value of the first number) to the second number.

By following these three steps, the values of the two numbers are effectively swapped.

3. Code Program


#include <iostream>
using namespace std;

int main() {
    double num1, num2, temp;

    // Input numbers
    cout << "Enter first number: "; // Prompt the user to input the first number
    cin >> num1; // Read the input and store it in num1
    cout << "Enter second number: "; // Prompt the user to input the second number
    cin >> num2; // Read the input and store it in num2

    // Swapping process
    temp = num1; // Store the value of num1 in a temporary variable (temp)
    num1 = num2; // Assign the value of num2 to num1
    num2 = temp; // Assign the stored value (from temp) to num2, effectively swapping the values

    // Display the swapped values
    cout << "After swapping, first number = " << num1 << " and second number = " << num2 << endl;

    return 0; // Indicate successful execution by returning 0
}

Output:

For an input of:
Enter first number: 10
Enter second number: 20
After swapping, first number = 20 and second number = 10

4. Step By Step Explanation

1. Headers and Namespace: We start by including the I/O stream library and use the standard namespace for input and output operations.

#include <iostream>
using namespace std;

2. Variable Declaration: Here, we declare three variables: two (num1 and num2) to hold the numbers we want to swap, and a third temp as the temporary variable for the swapping process.

    double num1, num2, temp;

3. User Input: The program uses cout to display prompts and cin to receive user input for the two numbers.

    // Input numbers
    cout << "Enter first number: "; // Prompt the user to input the first number
    cin >> num1; // Read the input and store it in num1
    cout << "Enter second number: "; // Prompt the user to input the second number
    cin >> num2; // Read the input and store it in num2

4. Swapping Logic:

- The value of num1 is stored in temp.- The value of num2 is then assigned to num1.- Finally, the value stored in temp (original value of num1) is assigned to num2.

    // Swapping process
    temp = num1; // Store the value of num1 in a temporary variable (temp)
    num1 = num2; // Assign the value of num2 to num1
    num2 = temp; // Assign the stored value (from temp) to num2, effectively swapping the values

5. Displaying the Result: The swapped values of the numbers are displayed using cout.

    // Display the swapped values
    cout << "After swapping, first number = " << num1 << " and second number = " << num2 << endl;

6. Program Termination: The program ends by returning 0, signifying successful execution.

    return 0; // Indicate successful execution by returning 0

Comments