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

void main() { int i=i++,j=j++,k=k++; printf(“%d%d%d”,i,j,k); }

1 Answers  


write a function to give demostrate the functionality of 3d in 1d. function prototye: change(int value,int indexX,int indexY,int indexZ, int [] 1dArray); value=what is the date; indexX=x-asix indexY=y-axis indexZ=z-axis and 1dArray=in which and where the value is stored??

0 Answers   Nagarro,


main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above

1 Answers   HCL,


main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }

1 Answers  


main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); }

2 Answers   Persistent,






#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }

1 Answers  


#define a 10 int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } void foo() { #undef a #define a 50 }

3 Answers  


union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4

3 Answers   HCL,


To reverse an entire text file into another text file.... get d file names in cmd line

0 Answers   Subex,


plz tell me the solution.......... in c language program guess any one number from 1 to 50 and tell that number within 8 asking question in yes or no...............

2 Answers   Wipro,


write a program to Insert in a sorted list

4 Answers   Microsoft,


main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

6 Answers  


Categories