Write a Program to Swap Two Number without Using Third Variable in Cpp.
Working
- Make Two Variable First,Second.
- Get Value from user.
- Display Number Before Swap
- Addition First Number & Second Number Move to First Number ( First = First + Second )
- Subtract Second Number From First Number Move to Second ( Second = First – Second )
- Subtract Second Number From First Number Move to First ( First = First – Second )
- Display Number After Swap
Program
//Swap Two without Third Variable Number in CPP #include<iostream.h> #include<conio.h> void main(){ clrscr(); int first_num=0,second_num=0; cout<<"\n Swap Two Number without using Third Variable"; cout<<"\n -------------------------------- "; cout<<"\n Enter the First Number : "; cin>>first_num; cout<<"\n Enter the Second Number : "; cin>>second_num; //Before Swap cout<<"\n Before Swap First Number : "<<first_num<<" Second Number : "<<second_num; //Swap Process first_num = first_num + second_num; second_num = first_num - second_num; first_num = first_num - second_num; //after Swap cout<<"\n After Swap First Number : "<<first_num<<" Second Number : "<<second_num; getch(); }
Output / Result
(Visited 148 times, 1 visits today)