How can we right a program in which we swap/interchange just two integers, remember that we use only two variables?
Swap/interchange variable c++ program question.
Â
Normally anybody can swap any variable. We can use any programming Language to do that job. If we use C language.
Â
First We need to declare a three variable like int x,y,temp;
Â
Suppose x=10;
Â
y=5;
Â
temp=0;
Â
When we swap it x comers value 5, and y comes Value 10.
Â
Do it as x= temp; /————— the value of x comes in temp now 10.
Â
X=y; / —————–Value of y comes x now.
Â
y= temp; / —————–value of temp means value of x previous 10 comes in y now.
Â
Result : value of x=5;
Â
Value of y=10.
Â
temp=0.
Â
Its easy in c programming. Anybody can do as easily.
Swap/interchange variable c++ program question.
Hi Bubble Jade,
Please find the below code to interchange / swap two variables in c++.
#include <iostream.h>
int main()
{
int a=3,b=4;
count<<" n Original value of a = "<<a<<" t b = "<<b;
a = a + b;
b = a – b;
a = a – b;
count<<" n Interchange value of a = "<<a<<" t b = "<<b;
return 0;
}
Thanks,
Sophia Taj
Swap/interchange variable c++ program question.
I've tried your program Sophia in interchanging two variables and looks like you've got it. Thanks! Very helpful indeed.
Thanks Sophia and techyv.