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

Only #2 will work. The others may over/under flow.

Is This Answer Correct ?    83 Yes 129 No

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

Answer / krishna

a=(a>b?a:b)

Is This Answer Correct ?    9 Yes 64 No

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

Answer / ankush

using ref keyword we can swap 2 number's

emaple
class swap
{
public void add(ref int a,ref int b)
{
int z;
z=a;
a=b;
b=z;
}
}
class swapdemo
{
static void main()
{
int x=10,y=5;
ankush obj=new ankush();
Console.writeline{"befor value's"+a+","+b};
obj.swap(ref x, ref y);
Console.writeline("after values"+a+","+b);
}
}

Is This Answer Correct ?    13 Yes 72 No

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

Answer / vinay_csjm

a=5,b=10
a=a-b;
b=a+b;
a=a+b;

Is This Answer Correct ?    184 Yes 390 No

Post New Answer

More C Code Interview Questions

char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp)

1 Answers  


main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }

2 Answers  


Is it possible to print a name without using commas, double quotes,semi-colons?

7 Answers  


4. Main() { Int i=3,j=2,c=0,m; m=i&&j||c&I; printf(“%d%d%d%d”,I,j,c,m); }

2 Answers   Broadridge,


Is the following code legal? struct a { int x; struct a b; }

1 Answers  






main() { static int var = 5; printf("%d ",var--); if(var) main(); }

1 Answers  


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,


find A^B using Recursive function

2 Answers  


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

0 Answers  


programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h

1 Answers  


Finding a number which was log of base 2

1 Answers   NetApp,


Derive expression for converting RGB color parameters to HSV values

1 Answers  


Categories