Swap Two Numbers With and Without Third Variable.

Swap Two Numbers With and Without Third Variable.

Swapping basically means interchanging values of the variables. Usually swapping numbers becomes easy with the third variable. But it can be also done without third variable. Variables always contain a junk value. That’s why initially we have to assign values to the variables. Once the variables are assigned certain values it becomes easy to swap those values. Let’s see how to swap two numbers with or without third variable.

1. Code

C program with third variable

//swapping of nos in C with third variable
#include<stdio.h>
int main()
{
    int a, b, swap;
    printf("\nEnter two nos:");
    scanf("%d%d",&a,&b);
    printf("\nValues before swapping :%d %d",a,b);
    //swapping of variables
    swap = a;
    a = b;
    b = swap;
    printf("\nValues after swapping :%d %d",a,b);
    return 0;
}

Without third variable in C

//swapping of nos in C without third variable
#include<stdio.h>
int main()
{
    int a, b;
    printf("\nEnter two nos:");
    scanf("%d%d",&a,&b);
    printf("\nValues before swapping :%d %d",a,b);
    //swapping of variables
    a = a + b;
    b = a - b;
    a = a - b;
    printf("\nValues after swapping :%d %d",a,b);
    return 0;
}

C++ program without third variable

//swapping of nos in C++ without third variable
#include<iostream>
using namespace std;
int main()
{
    int a, b;
    cout<<"\nEnter two nos:";
    cin>>a>>b;
    cout<<"\nValues before swapping :"<<a<<" "<<b;
    //swapping of variables
    a = a + b;
    b = a - b;
    a = a - b;
    cout<<"\nValues after swapping :"<<a<<" "<<b;
    return 0;
}

With third variable in C++

//swapping of nos in C++ with third variable
#include<iostream>
using namespace std;
int main()
{
    int a, b, swap;
    cout<<"\nEnter two nos:";
    cin>>a>>b;
    cout<<"\nValues before swapping :"<<a<<" "<<b;
    //swapping of variables
    swap = a;
    a = b;
    b = swap;
    cout<<"\nValues after swapping :"<<a<<" "<<b;
    return 0;
}

2. Output

Swap Two Numbers | With and Without Third Variable

Also see : Arithmetic operations or simple calculator in C/C++.

3. Program Parts

#include is a pre-processor directive. It is used to include header files.
stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
.h is an extension for header file.
int is data type. It indicates that the program returns a value.
main() is main function that indicates the compiler that the user written programs starts from here.
a,b,swap are just variable names.
print/cout is the keyword use to print a message.
return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation

Initially we declare three variables of integer type. Then we assign values to two variables. We display initial numbers given by user. The third variable swap stores the first number in it and second number is stored in a. Now the first number is stored in b.

For swapping numbers without third variable we use a different logic. The logic is first we add both numbers and store it in a. Then we subtract second number from first and store it in b. At last, we again subtract both the numbers and this time we store the answer in a. And that’s it, we have swapped our numbers.

Conclusion

That’s how we swap two numbers with or without third variable! If you have any other logic then tell us in the comment section below. Also if you liked this logic and explanation do share with your friends. Tell us what other programs would you like us to post.

For more such posts and daily updates follow us on instagram. Do visit again!

ErrorFreeProgram

2 COMMENTS

Leave a Reply

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