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

main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


how to test pierrot divisor

0 Answers  


main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\n”); }

1 Answers  


main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }

3 Answers  


How to count a sum, when the numbers are read from stdin and stored into a structure?

1 Answers  






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

1 Answers  


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

1 Answers  


#include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }

1 Answers  


Develop a routine to reflect an object about an arbitrarily selected plane

0 Answers  


main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }

4 Answers   CSC,


Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.

2 Answers   Mentor Graphics, Microsoft,


You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.

3 Answers  


Categories