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

rahul

Is This Answer Correct ?    0 Yes 1 No

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

Answer / deep

let a=5 , b=10
a=a-b
means a=5-10=-5
b=b+a
b=10+(-5)
b=5
a=b-a
a=5-(-5)
a=10

Is This Answer Correct ?    1 Yes 2 No

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

Answer / beeresh

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

Is This Answer Correct ?    0 Yes 1 No

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

Answer / d maniteja

let
a=x, b=y our aim is to get output as a=y&b=x;
program:
void main()
{
int a=x,b=y;
a=(a+b)+(a-b);
b=(a+b)-(a-b);
a=a/2;
b=b/2;
printf(("%d %d",a,b);
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / e.naveenkumar

without using third variable swap two nos
a=(a+b)-(b-a);

Is This Answer Correct ?    0 Yes 1 No

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

Answer / amit kumar

10 answer is perfect

Is This Answer Correct ?    0 Yes 1 No

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

Answer / gobinath

swap(int *a,int *b)
{
*a=*a-*b;
*b=*a+*b;
*a=*b-*a;

}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / sudha

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

1. b=b+a
2. a=b-a
3. b=b-a

Is This Answer Correct ?    1 Yes 2 No

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

Answer / suraj

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

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

2 Answers  


main() { extern out; printf("%d", out); } int out=100;

1 Answers  


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

4 Answers   CSC,


#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

2 Answers  


struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }

1 Answers  






main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error

1 Answers   HCL,


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

1 Answers   Value Labs,


main() { printf("%x",-1<<4); }

3 Answers   HCL, Sokrati, Zoho,


how to swap 3 nos without using temporary variable

4 Answers   Satyam,


int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

1 Answers  


What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql

0 Answers  


main() { int c=- -2; printf("c=%d",c); }

1 Answers   TCS,


Categories