Q:

C++ Program to Swap Two Numbers without using third variable

belongs to collection: C++ Basic Solved Programs

0

Write a C++ Program to Swap Two Numbers without using third variable. C++ Program to Swap Two Numbers without  third variable. Here’s simple Program to Swap Two Numbers without using temp variable in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here is source code of the C++ Program to Swap Two Numbers without using third variable. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

 
 

SOURCE CODE : :

/*  C++ Program to Swap Two Numbers without using third variable  */

#include <iostream>
using namespace std;

int main()
{

    int a,b ;

    cout<<"Enter 1st number :: ";
    cin>>a;
    cout<<"\nEnter 2nd number :: ";
    cin>>b;

    cout << "\nBefore swapping, Numbers are :: " << endl;
    cout << "\n\ta = " << a << ", b = " << b << endl;

    a = a + b;
    b = a - b;
    a = a - b;

    cout << "\nAfter swapping, Numbers are :: " << endl;
    cout << "\n\ta = " << a << ", b = " << b << endl;

    return 0;
}

Output : :


/*  C++ Program to Swap Two Numbers without using third variable  */

Enter 1st number :: 5

Enter 2nd number :: 9

Before swapping, Numbers are ::

        a = 5, b = 9

After swapping, Numbers are ::

        a = 9, b = 5

Process returned 0

Above is the source code for C++ Program to Swap Two Numbers without using third variable which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Find Size of Int Float Double and C... >>
<< C++ Program to Find Quotient and Remainder of 2 nu...