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

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

Is This Answer Correct ?    0 Yes 1 No

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

Answer / shruti dengre

answer no. 6th is is correct

Is This Answer Correct ?    0 Yes 1 No

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

Answer / suseela

for ex x=10,y=20
x=x+y=30(i.e 10+20)
y=x-y=10(i.e 30-20)
x=x-y=20(i.e 30-10)

Is This Answer Correct ?    0 Yes 1 No

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

Answer / shubham

#include<stdio.h>
#imclude<conio.h>
main()
{
int a,b;
printf("enter two number");
scanf("%d%d",&a,&b);
b=a-b;
a=a-b;
b=a+b;
printf("after swaping &d",swap);
getch();
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / lokesh

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

Is This Answer Correct ?    0 Yes 1 No

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

Answer / praveen kumar telluri

b=a*b/(a=b);

Is This Answer Correct ?    0 Yes 1 No

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

Answer / naveen

Using a single line statement:

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

Is This Answer Correct ?    0 Yes 1 No

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

Answer / hamsa

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

ex a=2, b=4

a=2+4
b=6-4
a=6-2


ANS a=4
b=4

Is This Answer Correct ?    0 Yes 1 No

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

Answer / debjyoti talukder

a=a+b;
if(a>b)//only the smaller can b subtracted from greater...
b=a-b://...reverse is not possible
else
b=b-a;
a=a-b;

Is This Answer Correct ?    0 Yes 1 No

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

Answer / swapnil r

Swapping two variables

1. #define SWAP(x,y) x^=y^=x^=y

2. a=a+b;
b=a-b;
a=a-b;

3. use xor to swap

a = a^b
b= a^b
a= a^b
4. a=a*b;
b=a/b;
a=a/b;

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More C Code Interview Questions

Sir... please give some important coding questions asked by product companies..

0 Answers  


main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

1 Answers  


main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

1 Answers  


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

1 Answers  


Is the following code legal? struct a { int x; struct a *b; }

2 Answers  






How to access command-line arguments?

4 Answers  


/*what is the output for*/ void main() { int r; printf("Naveen"); r=printf(); getch(); }

4 Answers  


main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); }

2 Answers  


Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

5 Answers   IITR, Microsoft, Nike,


how to delete an element in an array

2 Answers   IBM,


main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above

4 Answers   HCL, LG,


main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }

4 Answers   CSC,


Categories