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 / mohit prakash saxena

Yes IT is absolutely correct

Is This Answer Correct ?    0 Yes 2 No

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

Answer / yogendra

a=5
a=5,b=10

a=a+b
a=10+5=15
a=15
b=a-b
b=15-10
b=5


a=a-b
a=15-5
a=10 and b=5


a=10 and b=5

Is This Answer Correct ?    0 Yes 2 No

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

Answer / praveen kumar kesani

x=10,y=20;
x=x*y;
y=x/y;
x=x/y;

after swapping : x=20,y=10

Is This Answer Correct ?    2 Yes 5 No

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

Answer / ruchi

let the two numbers are a & b

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

let a=5 & b=10
a=a+b=15
b=a-b=15-10=5
b=5

a=a-b=15-5=10
hence a & b become 10 & 5

Is This Answer Correct ?    0 Yes 3 No

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

Answer / balusamy

Try this answer for reverse a string


#include<stdio.h>
void main()
{
char array[50] = "thgir si rewsna ym";
int len,i,j,temp;

len = strlen(array);

for(i = 0, j = len - 1; i < j; i++, j--)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}

printf("%s\n",array);
}

Is This Answer Correct ?    0 Yes 4 No

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

Answer / smita

suppose a=5 and b=10
a=a*b ==>a=50
b=a/b ==>b=5;
a=a/b ==>a=10;

Is This Answer Correct ?    1 Yes 5 No

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

Answer / ashok

The first two answers are correct. Third will FAIL in the
case the second num is 0...Please do not post wrong answer

Is This Answer Correct ?    3 Yes 8 No

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

Answer / sonya

first & third answers are correct.

Is This Answer Correct ?    3 Yes 8 No

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

Answer / some guy

declare a fourth variable and use that.
I dont understand why you need to do any of the above ??? if fourth is a problem, declare fifth and so on...

Is This Answer Correct ?    2 Yes 7 No

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

Answer / robince kumar

//C++ code..
void main()
{
int number1,number2;
cout<<"Enter 1st number;
cin>>number1;
cout<<"Enter 2nd number;
cin>>number2;
number1=number1*number2;
number2=number1/number2;
number1=number1/number2;
getch();
}

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More C Code Interview Questions

void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }

1 Answers  


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  


Write a C program to add two numbers before the main function is called.

11 Answers   Infotech, TC,


#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

3 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,






To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

0 Answers  


Is the following code legal? struct a { int x; struct a b; }

1 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,


#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }

1 Answers  


main() { printf("\nab"); printf("\bsi"); printf("\rha"); }

3 Answers  


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

0 Answers   United Healthcare, Virtusa,


void main() { int const * p=5; printf("%d",++(*p)); }

3 Answers   Infosys, Made Easy, State Bank Of India SBI,


Categories