codescracker
c++

C++ Program to Swap Two Numbers



« Previous Program Next Program »

In this article, you will learn and get code to swap any two numbers entered by user at run-time using a C++ program. Here are the list of programs available in this article:

Swap Two Numbers using Third Variable

To swap two numbers in C++ programming, you have to ask from user to enter the two numbers. The entered two numbers gets stored in two variables say numOne and numTwo. Now swap both numbers using its variable as shown in the program given below:

// C++ Swap two Numbers using Third Variable
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int numOne, numTwo, temp;
    cout<<"Enter the First Number: ";
    cin>>numOne;
    cout<<"Enter the Second Number: ";
    cin>>numTwo;
    cout<<"\nBefore Swap:\n";
    cout<<"First Number = "<<numOne<<"\tSecond Number = "<<numTwo;
    temp = numOne;
    numOne = numTwo;
    numTwo = temp;
    cout<<"\n\nAfter Swap:\n";
    cout<<"First Number = "<<numOne<<"\tSecond Number = "<<numTwo;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program to swap two numbers

Now supply inputs say 10 as first and 20 as second number. Press ENTER to swap these two numbers and print the value of both variables (that stores these two values say numOne and numTwo) before and after swap as shown in the snapshot given below:

swap two numbers using third variable c++

We've used a third variable say temp that helps in swapping of two numbers. That is, the value of numOne gets stored in temp, value of numTwo gets stored in numOne. And finally the value of temp (numOne's value) gets stored in numTwo. In this way, swapping gets performed.

Swap Two Numbers without using Third Variable

To swap two numbers without using third variable, just replace the following three lines of code (from previous program):

temp = numOne;
numOne = numTwo;
numTwo = temp;

with the block of code given below:

numOne = numOne + numTwo;
numTwo = numOne - numTwo;
numOne = numOne - numTwo;

Also remove the declaration of temp variable from the program. Rest of the things will be same.

Let's suppose if user enters 10 and 20 as first and second number. Therefore numOne=10 and numTwo=20. Now using above three lines of code. That is, after executing the following statement:

numOne = numOne + numTwo;

10+20 or 30 gets initialized to numOne. So numOne=30. And using the following (second) statement:

numTwo = numOne - numTwo;

30-20 or 10 gets initialized to numTwo. So numTwo=10. And using the third statement, that is:

numOne = numOne - numTwo;

30-10 or 20 gets initialized to numOne. So numOne=20. In this way swapping gets performed without using third variable.

Swap Two Numbers using Function (Call by Reference)

This program does the same job as of previous program. But using user-defined function named swapFun(), uses call-by-reference way. Let's have a look at the program first:

// C++ Swap two Numbers using Function (call by reference)
// ----codescracker.com----

#include<iostream>
using namespace std;
void swapFun(int *, int *);
int main()
{
    int numOne, numTwo;
    cout<<"Enter the First Number: ";
    cin>>numOne;
    cout<<"Enter the Second Number: ";
    cin>>numTwo;
    cout<<"\nBefore Swap:\n";
    cout<<"First Number = "<<numOne<<"\tSecond Number = "<<numTwo;
    swapFun(&numOne, &numTwo);
    cout<<"\n\nAfter Swap:\n";
    cout<<"First Number = "<<numOne<<"\tSecond Number = "<<numTwo;
    cout<<endl;
    return 0;
}
void swapFun(int *nOne, int *nTwo)
{
    int temp;
    temp = *nOne;
    *nOne = *nTwo;
    *nTwo = temp;
}

In above program, inside the main() function, swapFun() gets called. It takes two arguments of int pointer type. Therefore we've passed address of both variables say numOne and numTwo that holds first and second number entered by user.

Note - The * is called as value at and & is called as address of operator.

In this way, the address of both values (entered by user) gets passed to the function swapFun(). Now using * operator, we've accessed the value at address, and swapped the numbers.

Note - To learn more about function, refer to Function in C++ article to get every required things about it.

Swap Two Numbers using Class

This program uses class and object, an object-oriented feature of C++ to swap two numbers.

// C++ Swap two Numbers using Class and Object
// ----codescracker.com----

#include<iostream>
using namespace std;
class CODESCRACKER
{
    public:
        void swapFun(int *, int *);
};
void CODESCRACKER::swapFun(int *nOne, int *nTwo)
{
    int temp;
    temp = *nOne;
    *nOne = *nTwo;
    *nTwo = temp;
}
int main()
{
    int numOne, numTwo;
    cout<<"Enter the First Number: ";
    cin>>numOne;
    cout<<"Enter the Second Number: ";
    cin>>numTwo;
    cout<<"\nBefore Swap:\n";
    cout<<"First Number = "<<numOne<<"\tSecond Number = "<<numTwo;
    CODESCRACKER c;
    c.swapFun(&numOne, &numTwo);
    cout<<"\n\nAfter Swap:\n";
    cout<<"First Number = "<<numOne<<"\tSecond Number = "<<numTwo;
    cout<<endl;
    return 0;
}

Here is its sample run with user input, 50 as first and 60 as second number:

swap two numbers using class c++

An object c of type (class) CODESCRACKER is created inside the main() function. Now through this object, we've called the member function swapFun() of the class CODESCRACKER using the dot (.) operator. Now the function operates in similar way as normal function operates.

Note - To get its in-depth detail, then refer to class and object

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker