3/03/2015

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

Problem :- 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 .

Solution :-

#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:-

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

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment