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

find A^B using Recursive function

2 Answers  


prog. to produce 1 2 3 4 5 6 7 8 9 10

4 Answers   TCS,


main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); }

1 Answers  


Write a C function to search a number in the given list of numbers. donot use printf and scanf

5 Answers   Honeywell, TCS,


write a origram swaoing valu without 3rd variable

2 Answers  






main() { 41printf("%p",main); }8

1 Answers  


Is this code legal? int *ptr; ptr = (int *) 0x400;

1 Answers  


main() { extern int i; i=20; printf("%d",sizeof(i)); }

2 Answers  


void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }

1 Answers  


program to find magic aquare using array

4 Answers   HCL,


Write a program that reads a dynamic array of 40 integers and displays only even integers

2 Answers  


struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }

1 Answers  


Categories