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 / saddi srikanth

if x=14, y=18
x=x+y;
now x=14+18=32;
y=x-y;
now y=32-18=14;
now again y=14
x=x-y;
now x=32-14=18
final answer is:
x=18, and y=14
u can try this formule by taking any values for x and y.

Is This Answer Correct ?    4 Yes 5 No

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

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

1 Answers  


#include <stdio.h> int main(void) { int a=4, b=2; a=b<<a+b>>2 ; printf("%d",a); return 0; }

0 Answers   Student,


int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

2 Answers  


main() { int *j; { int i=10; j=&i; } printf("%d",*j); }

9 Answers   HCL, Wipro,


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  






All the combinations of prime numbers whose sum gives 32

1 Answers   HHH,


int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }

1 Answers  


int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too

1 Answers  


main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above

4 Answers   HCL, LG,


#define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); }

1 Answers  


I need your help, i need a Turbo C code for this problem.. hope u'll help me guys.? Your program will have a 3x3 array. The user will input the sum of each row and each column. Then the user will input 3 values and store them anywhere, or any location or index, temporarily in the array. Your program will supply the remaining six (6) values and determine the exact location of each value in the array. Example: Input: Sum of row 1: 6 Sum of row 2: 15 Sum of row 3: 24 Sum of column 1: 12 Sum of column 2: 15 Sum of column 3: 18 Value 1: 3 Value 2: 5 Value 3: 6 Output: Sum of Row 1 2 3 6 4 5 6 15 7 8 9 24 Sum of Column 12 15 18 Note: Your program will not necessary sort the walues in the array Thanks..

0 Answers  


Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2])

1 Answers  


Categories