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

what is the output of following program ? void main() { int i=5; printf("%d %d %d %d %d ",i++,i--,++i,--i,i); }

10 Answers  


main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

5 Answers   HCL,


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates.

19 Answers   Amazon, BITS, Microsoft, Syncfusion, Synergy, Vector,


main() { int c=- -2; printf("c=%d",c); }

1 Answers   TCS,


What is data _null_? ,Explain with code when u need to use it in data step programming ?

0 Answers   Abbott,






What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

1 Answers  


given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit

8 Answers  


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() { 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,


main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


Cluster head selection in Wireless Sensor Network using C programming language.

0 Answers  


main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

1 Answers  


Categories