Swap/interchange variable c++ program question.

Asked By 30 points N/A Posted on -
qa-featured

How can we right a program in which we swap/interchange just two integers, remember that we use only two variables?

SHARE
Best Answer by Sophia Taj
Answered By 0 points N/A #116499

Swap/interchange variable c++ program question.

qa-featured

 

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.
Best Answer
Best Answer
Answered By 0 points N/A #116500

Swap/interchange variable c++ program question.

qa-featured

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

Answered By 30 points N/A #116501

Swap/interchange variable c++ program question.

qa-featured

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.

Login/Register to Answer

Related Questions