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

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.

6 Answers   Fusion Systems GmbH,


write a c program to print magic square of order n when n>3 and n is odd?

1 Answers   HCL,


main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


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

3 Answers  


posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,






main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

3 Answers   GNITC,


Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,


How we will connect multiple client ? (without using fork,thread)

3 Answers   TelDNA,


typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

1 Answers  


void main() { int c; c=printf("Hello world"); printf("\n%d",c); }

2 Answers  


What are the files which are automatically opened when a C file is executed?

1 Answers  


how can i search an element in an array

2 Answers   CTS, Microsoft, ViPrak,


Categories