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

what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

2 Answers  


#define a 10 int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } void foo() { #undef a #define a 50 }

3 Answers  


How to read a directory in a C program?

4 Answers  


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

3 Answers   Cisco, HCL,






main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000

3 Answers   HCL,


All the combinations of prime numbers whose sum gives 32

1 Answers   HHH,


why nlogn is the lower limit of any sort algorithm?

0 Answers  


main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


main() { 41printf("%p",main); }8

1 Answers  


main() { int i = 3; for (;i++=0;) printf(ā€œ%dā€,i); }

1 Answers   CSC,


{ int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

4 Answers  


Categories