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 / swapna

Hi this question was asked in my interview.
Ans is :

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

Any other solution?

Is This Answer Correct ?    1770 Yes 223 No

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

Answer / guest

use xor to swap

a = a^b
b= a^b
a= a^b

Is This Answer Correct ?    550 Yes 165 No

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

Answer / somasekhar

Ans is :

a=a*b;
b=a/b;
a=a/b;

Is This Answer Correct ?    536 Yes 185 No

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

Answer / kiran

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

Is This Answer Correct ?    302 Yes 65 No

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

Answer / mohit

x = x + y;

y = x - y;

x = x - y;

Is This Answer Correct ?    113 Yes 26 No

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

Answer / bruce tuskey

Only the Xor answer (#2) is correct (in cases where the
variables are the same size). With all the other answers,
you could run into over/under flow problems.
A = 01111111
B = 01111101

A = A^B = 00000010
B = A^B = 01111111 (Original A)
A = A^B = 01111101 (Original B)

Is This Answer Correct ?    95 Yes 21 No

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

Answer / partha

a=b-a+(b=a);

Is This Answer Correct ?    119 Yes 78 No

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

Answer / gowtham

2 one will work correct

Is This Answer Correct ?    84 Yes 45 No

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

Answer / pavan.

void main()
{
int a,b;
printf("Enter two number : ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The swap number is : %d %d",a,b);
getch();
}

Is This Answer Correct ?    46 Yes 12 No

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

Answer / yash

Answer no 12 is wrong. the logic fails when a=0;

Is This Answer Correct ?    44 Yes 17 No

Post New Answer

More C Code Interview Questions

#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?

2 Answers  


main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }

1 Answers  


main() { signed int bit=512, i=5; for(;i;i--) { printf("%d\n", bit >> (i - (i -1))); } } a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d. 256, 256, 256, 256, 256

2 Answers   HCL,


main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above

1 Answers   HCL,


How we will connect multiple client ? (without using fork,thread)

3 Answers   TelDNA,






main() { int i = 3; for (;i++=0;) printf(“%d”,i); }

1 Answers   CSC,


#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }

1 Answers  


main() { int i, n; char *x = “girl”; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(“%s\n”,x); x++; } }

2 Answers  


Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,


How we print the table of 2 using for loop in c programing?

14 Answers   HCL, Wipro,


main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); }

1 Answers  


writte a c-programm to display smill paces

2 Answers  


Categories