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

Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable.

5 Answers   Amazon, Microsoft,


What is data _null_? ,Explain with code when u need to use it in data step programming ?

0 Answers   Abbott,


pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"

2 Answers  


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

1 Answers  


Cluster head selection in Wireless Sensor Network using C programming language.

0 Answers  






void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(“%d”,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(“%d”,*cptr); }

1 Answers  


write a c program to Create employee record by taking details like name, employee id, address and phone number. While taking the phone number, take either landline or mobile number. Ensure that the phone numbers of the employee are unique. Also display all the details

2 Answers   TCS,


void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }

1 Answers  


char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp)

1 Answers  


#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  


C statement to copy a string without using loop and library function..

2 Answers   Persistent, TCS,


main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

6 Answers  


Categories