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

b=a*b/(a=b);

Is This Answer Correct ?    1 Yes 8 No

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

Answer / vinay kumar shukla

i am not giving answer
all above answer for swaping integer type variables without
third
i want answer in string type

Is This Answer Correct ?    6 Yes 15 No

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

Answer / saumya

Thanks a lot guys. It is giving very good analysis.

Is This Answer Correct ?    7 Yes 19 No

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

Answer / koneru gowtham

plz check 3 aswer is perfect, 2 one mat be wrong in some cases

Is This Answer Correct ?    47 Yes 63 No

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

Answer / kumar

Using Assembly language( Using Accumulator)...Without using
any arithmatic...without using any Pointer...without
declaring third varible

int a = 20;
int b = 10;
__asm
{
mov EAX,b
push EAX
mov EAX,a
mov b,EAX
pop EAX
mov a,EAX
}

Is This Answer Correct ?    17 Yes 33 No

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

Answer / meenama

The first two answers are correct. Third will FAIL in the
case the second num is 0. The #19 one still uses a third
variable.

Is This Answer Correct ?    5 Yes 21 No

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

Answer / mangesh

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
clrscr();
printf("\nEnter two numbers:");
scanf("%d%d",&a,&b);
printf("\nThe numbers after swapping are %d %d",b,a);
getch();
return 0;
}

Is This Answer Correct ?    0 Yes 18 No

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

Answer / rehan

#include<stdio.h>
main()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
printf("The values before swapping are\n%d %d\n",a,b);
swap(&a,&b);
printf("The values after swapping are\n%d %d\n",a,b);
getch();
}

swap(*x,*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}

Is This Answer Correct ?    5 Yes 24 No

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

Answer / divakar

a=a*b/(b=a);

Is This Answer Correct ?    25 Yes 57 No

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

Answer / javaskills

x = y | (y=x);

Is This Answer Correct ?    11 Yes 54 No

Post New Answer

More C Code Interview Questions

union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0

1 Answers   HCL,


main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }

2 Answers  


main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }

1 Answers  


main() { char a[4]="HELL"; printf("%s",a); }

3 Answers   Wipro,


code of a program in c language that ask a number and print its decremented and incremented number.. sample output: input number : 3 321123

1 Answers   HCL,






find A^B using Recursive function

2 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   TCS,


respected sir, i did my MCA in 2013 when i am going to attend to an interview i was asked about my project how will i explain my project could please help me in this and my project title is "Social Networking Site For Social Responsibility"

1 Answers   Genpact, Ozdocs,


void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(ā€œ%dā€, i); }

2 Answers  


void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); }

1 Answers  


main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

9 Answers   CSC, GoDB Tech, IBM,


Who could write how to find a prime number in dynamic array?

1 Answers  


Categories