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

could you please send the program code for multiplying sparse matrix in c????

0 Answers  


int a = 10 + 10 .... ,... A = A * A What would be the value of A? The answer is 120!! Could anyone explain this to me.

2 Answers   Bosch, eInfochips, HCL, IHCL,


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).

0 Answers   CDAC, College School Exams Tests,


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

2 Answers  






how to return a multiple value from a function?

5 Answers   Wipro,


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above

3 Answers   HCL,


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() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

2 Answers  


main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }

1 Answers  


Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];

1 Answers  


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

2 Answers   HCL,


Categories