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 / satyaprakash tripathi

a=a-b;
b=a+b;
a=b-a;

Is This Answer Correct ?    3 Yes 4 No

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

Answer / avanthi kothakonda

i want to swapping without using any third variable and
opartion

Is This Answer Correct ?    0 Yes 1 No

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

Answer / mohit prakash saxena

2 answer will not work if the value of a is +ve and b is negative try for a=2 and b=-2

Is This Answer Correct ?    2 Yes 3 No

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

Answer / yasser

to swap in a single line..

a=a+b-(b=a);

Is This Answer Correct ?    2 Yes 3 No

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

Answer / swamy

class SwapDemo
{
int a,b;
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
}
public static void main(String args[])
{
SwapDemo s=new SwapDemo();
s.swap(12,14);
}
}

Is This Answer Correct ?    1 Yes 2 No

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

Answer / poojamnagal

Num1 = 5
Num2 = 7

Num1 = 5+7 = 12
Num2 = 12- 7 = 5
Num1 = 12-5 = 7

So, Now Num1 = 7
Num2 = 5

Is This Answer Correct ?    1 Yes 2 No

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

Answer / anoop pandey

a=a-b
b=a+b
a=b-a

Is This Answer Correct ?    2 Yes 3 No

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

Answer / mujtaba

answer 1 is the right 1:i.e

a=a+b;
b=a-b;
a=a-b;

Is This Answer Correct ?    1 Yes 2 No

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

Answer / mahesh

class swap_two_verible_use
{
public static void main(String args[])
{
int a=50;
int b=20;
System.out.println("A = "+a);
System.out.println("B = "+b);
System.out.println("-----------------");
System.out.println("After swiping");

a=a+b;
b=a-b;
a=a-b;
System.out.println("A = "+a);
System.out.println("B = "+b);
}
}

Is This Answer Correct ?    1 Yes 2 No

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

Answer / guest

a = a + b;
a = a - b;
b = b - a;

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Code Interview Questions

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,


main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }

1 Answers  


main() { signed int bit=512, i=5; for(;i;i--) { printf("%d\n", bit >> (i - (i -1))); } } a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d. 256, 256, 256, 256, 256

2 Answers   HCL,


main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above

3 Answers   HCL,


write a simple calculator c program to perform addition, subtraction, mul and div.

0 Answers   United Healthcare, Virtusa,






How to reverse a String without using C functions ?

33 Answers   Matrix, TCS, Wipro,


main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

1 Answers  


Sorting entire link list using selection sort and insertion sort and calculating their time complexity

1 Answers   Infosys, Microsoft, NetApp,


main() { int a[10]; printf("%d",*a+1-*a+3); }

1 Answers  


main() { clrscr(); } clrscr();

2 Answers  


main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above

5 Answers   HCL,


plz send me all data structure related programs

2 Answers  


Categories