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

Implement a t9 mobile dictionary. (Give code with explanation )

1 Answers   Amazon, Peak6, Yahoo,


write a origram swaoing valu without 3rd variable

2 Answers  


main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above

3 Answers   BrickRed, HCL,


void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above

2 Answers   HCL,


why nlogn is the lower limit of any sort algorithm?

0 Answers  






char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }

1 Answers  


# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


Write a program to receive an integer and find its octal equivalent?

7 Answers  


main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10

4 Answers   HCL,


Display the time of the system and display the right time of the other country

1 Answers  


main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

2 Answers  


hello sir,is there any function in C that can calculate number of digits in an int type variable,suppose:int a=123; 3 digits in a.what ll b answer?

6 Answers  


Categories