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

void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(ā€œ%dā€,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(ā€œ%dā€,*cptr); }

1 Answers  


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

2 Answers  


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

1 Answers   TCS,


main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

1 Answers  


how to print 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 using any loop(for or while) only once(only 1 loop) and maximum 2 variables using C.

19 Answers   Cap Gemini, Infosys,






String reverse with time complexity of n/2 with out using temporary variable.

10 Answers   NetApp, Symantec,


write a c program to print magic square of order n when n>3 and n is odd?

1 Answers   HCL,


Give a one-line C expression to test whether a number is a power of 2.

10 Answers   Microsoft,


Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?

0 Answers   Honeywell,


There is a lucky draw held every day. if there is a winning number eg 1876,then all possible numbers like 1867,1687,1768 etc are the numbers that match irrespective of the position of the digit. Thus all these numbers qualify fr the lucky draw prize Assume there is no zero digit in any numbers. write a program to show all the possible winning numbers if a "winning number"is passed as an arguments to the function.

1 Answers   Nagarro,


what is brs test reply me email me kashifabbas514@gmail.com

0 Answers  


main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


Categories