How to swap two variables, without using third variable ?

Answers were Sorted based on User's Feedback



How to swap two variables, without using third variable ?..

Answer / mohit prakash saxena

Yes IT is absolutely correct

Is This Answer Correct ?    0 Yes 2 No

How to swap two variables, without using third variable ?..

Answer / yogendra

a=5
a=5,b=10

a=a+b
a=10+5=15
a=15
b=a-b
b=15-10
b=5


a=a-b
a=15-5
a=10 and b=5


a=10 and b=5

Is This Answer Correct ?    0 Yes 2 No

How to swap two variables, without using third variable ?..

Answer / praveen kumar kesani

x=10,y=20;
x=x*y;
y=x/y;
x=x/y;

after swapping : x=20,y=10

Is This Answer Correct ?    2 Yes 5 No

How to swap two variables, without using third variable ?..

Answer / ruchi

let the two numbers are a & b

a=a+b
b=a-b
a=a-b

let a=5 & b=10
a=a+b=15
b=a-b=15-10=5
b=5

a=a-b=15-5=10
hence a & b become 10 & 5

Is This Answer Correct ?    0 Yes 3 No

How to swap two variables, without using third variable ?..

Answer / balusamy

Try this answer for reverse a string


#include<stdio.h>
void main()
{
char array[50] = "thgir si rewsna ym";
int len,i,j,temp;

len = strlen(array);

for(i = 0, j = len - 1; i < j; i++, j--)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}

printf("%s\n",array);
}

Is This Answer Correct ?    0 Yes 4 No

How to swap two variables, without using third variable ?..

Answer / smita

suppose a=5 and b=10
a=a*b ==>a=50
b=a/b ==>b=5;
a=a/b ==>a=10;

Is This Answer Correct ?    1 Yes 5 No

How to swap two variables, without using third variable ?..

Answer / ashok

The first two answers are correct. Third will FAIL in the
case the second num is 0...Please do not post wrong answer

Is This Answer Correct ?    3 Yes 8 No

How to swap two variables, without using third variable ?..

Answer / sonya

first & third answers are correct.

Is This Answer Correct ?    3 Yes 8 No

How to swap two variables, without using third variable ?..

Answer / some guy

declare a fourth variable and use that.
I dont understand why you need to do any of the above ??? if fourth is a problem, declare fifth and so on...

Is This Answer Correct ?    2 Yes 7 No

How to swap two variables, without using third variable ?..

Answer / robince kumar

//C++ code..
void main()
{
int number1,number2;
cout<<"Enter 1st number;
cin>>number1;
cout<<"Enter 2nd number;
cin>>number2;
number1=number1*number2;
number2=number1/number2;
number1=number1/number2;
getch();
}

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More C Code Interview Questions

main() { extern int i; i=20; printf("%d",sizeof(i)); }

2 Answers  


#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }

3 Answers  


#include<stdio.h> #include<conio.h> void main() { int a=(1,2,3,(1,2,3,4); switch(a) { printf("ans:"); case 1: printf("1");break; case 2: printf("2");break; case 3: printf("1");break; case 4: printf("4");break; printf("end"); } getch(); }

0 Answers  


#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

1 Answers  


main() { int i=400,j=300; printf("%d..%d"); }

3 Answers  


Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector

2 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   TCS,


main() { printf("\nab"); printf("\bsi"); printf("\rha"); }

3 Answers  


main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

7 Answers  


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

1 Answers  


main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }

2 Answers  


Develop a routine to reflect an object about an arbitrarily selected plane

0 Answers  


Categories