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

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

2 Answers  


main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }

2 Answers  


int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr)); }

2 Answers   CSC,


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 a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } }

1 Answers  


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


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

1 Answers  


void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; }

2 Answers  


main() { clrscr(); } clrscr();

2 Answers  


struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above

2 Answers   HCL,


WAP to display 1,2,3,4,5........N

2 Answers  


Write a program to model an exploding firecracker in the xy plane using a particle system

0 Answers   HCL,


Categories