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

Write a program using one dimensional array to assign values and then display it on the screen. Use the formula a[i]=i*10 to assign value to an element.

1 Answers   Samar State University,


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

1 Answers  


#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

2 Answers   CNSI,


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  


void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); }

1 Answers  






Write a C program to print ‘Campus Force training’ without using even a single semicolon in the program.

3 Answers   Wipro,


why nlogn is the lower limit of any sort algorithm?

0 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  


write a program to count the number the same (letter/character foreg: 's') in a given sentence.

2 Answers  


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above

4 Answers   Corporate Society, HCL,


why the range of an unsigned integer is double almost than the signed integer.

1 Answers  


main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above

2 Answers   HCL,


Categories