HomeC++C++ Program to Swap Two Numbers

C++ Program to Swap Two Numbers

In this post, you’ll learn how to write a Program to Swap Two Number in C++.

This lesson will help you learn how to Swap Two Numbers, with a simple assignment and substitution method using the C++ language. Let’s look at the below source code.

How to to Swap Two Numbers in C++?

RUN CODE SNIPPET

Source Code

#include <iostream>
using namespace std;
int main()
{
    int a = 5, b = 10, temp;
    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    temp = a;
    a = b;
    b = temp;
    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    return 0;
}

Output

Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5

#include <iostream>

  • This line which is called the header file. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code. The <iostream> file stands for Input and Output statement.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. First declare the variables (a, b, temp) as integers and assign the values –  a = 5 b = 10;.
  2. Next display the output statements with the cout and the Insertion Operator ‘ << ‘.
  3. When using sentences, write them within quotes ” “, " Before swapping. " and display the values with ‘<<‘ before swapping
  4.  Now we can work on how to swap the numbers, first we give the function statement a = temp; this function assigns temp = 5.
  5. Next the function statement b = a; assigns a = 10, finally the function statement temp = b; assigns b = 5.
  6. The numbers are swapped by first emptying the variables, to empty them we assign their value to another empty variable, now we can swap the values and re-assign them in the other variable.
  7. Using the output statement cout and the respective characters, to display the answer after swapping.
  8. End the function with return 0; this returns the function to main( ).

Now that you have understood how the source code performs its function, try it yourself with different values to it understand better.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured as to receive the values from the user rather than include the values directly in the code.

Another addition to this code, is the swapping function. Here we are using a mathematical operation to perform the swapping.

RUN CODE SNIPPET
#include <iostream>
using namespace std;
int main()
{
    int a, b;
    cin>> a;
    cin>> b;
    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    a = a + b;
    b = a - b;
    a = a - b;
    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    return 0;
}

Input

12
20

Output

Before swapping.
a = 12, b = 20

After swapping.
a = 20, b = 12
  • The structure is similar to the previous code, the extra function we include is the cin >> a; and cin >> b; , this statement is used to collect and store the values that the user enters in the assigned integer.
  • We use << a << b to display the stored values in the output.
  • To explain the Swapping function, we start by giving input values and assigning a = 12, b = 20.
  • First a = a + b; that is a = 12 + 20;  a = 32
  • Next b = a - b; that is b = 32 – 20; b = 12
  • Finally a = a - b; that is a = 32 – 12; a = 20, now when we display the answer a = 20 and b = 12.
  • The rest of the statements and functions are similar, to Swap Two Numbers in C++.

Share:

Leave a Reply

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022