Q:

C++Program To Swap A Number Without Using Third Variable

belongs to collection: Simple Programs in C++ Programming

0

Write A Program To Swap A Number Without Using Third Variable

Logic :-

 Swapping A two Number without Using Third Variable Is A simple logic that i am Explaining below .

Example :- a=10 And b=20 .

Step 1:- first add both number like a=a+b so a value is 30.

Step 2:- Now sub-struct b=a-b so b value is 10.

Step 3:- Finally a=a-b so 30-10=20 so a value is 20.

finally Number is swap .

All Answers

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

#include <iostream>
using namespace std;

int main()
{
    int first,second;
    
 cout<<"Enter The First And Second Variable :\n\n";
    cin>>first>>second;
    
 first=first+second;
    second=first-second;
    first=first-second;
    
 cout<<"\nFirst Number is = "<<first<<"\nSecond Number Is = "<<second<<endl;
    return 0;

}

 

Output:

Enter The First And Second Variable :

10

20

First Number is = 20

 

Second Number Is = 10

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

total answers (1)

C++ Program To Find The Address Of Variable... >>
<< C++ Program To Read Integer (N) And Print First Th...