how to swap two numbers with out using temp variable
Answers were Sorted based on User's Feedback
Answer / sachin patil
a = 5;
b = 10;
a = a+b;
b = a-b;
a = a-b;
Is This Answer Correct ? | 55 Yes | 5 No |
Answer / vishal gupta
well, use math:
x=5;
y=7;
x = x * y;
y = x / y;
x = x / y;
And the numbers have been swapped without the use of an
extra variable. I hope it helped.
Is This Answer Correct ? | 44 Yes | 8 No |
Answer / guest
another way is using bitwise XOR (^).
a=9; //a=1001
b=5; //b=0101
a=a^b; //a=1100
b=a^b; //b=1001
a=b^a; //a=0101
Is This Answer Correct ? | 29 Yes | 6 No |
Answer / a.g.dhivyalakshmi
//swaping of 2 numbers without using temp variable
//a=5 & b=6
a=a+b; //a=30
b=a-b; //b=5
a=a-b; //a=6
//Thus two numbers are swapped
// or
//a=5 & b=6
a=a*b; //a=30
b=a/b; //b=5
a=a/b; //a=6
//Thus two numbers are swapped
Is This Answer Correct ? | 8 Yes | 0 No |
Answer / raja
if a=10& b=20
a=a+b //a=30
b=a-b //b=10
a=a-b //a=20
finally we got a=20,b=10
Is This Answer Correct ? | 10 Yes | 3 No |
Answer / vaibhav meena
Its simple:
For Integer Values
A^=B^=A
and with arithmetic operators :
All the above answers are right
Is This Answer Correct ? | 7 Yes | 3 No |
Answer / pranav
//swaping of 2 numbers without using temp variable
//a=5 & b=6
a=a+b; //a=30
b=a-b; //b=5
a=a-b; //a=6
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / lucky
All the above ans. are nice.
I proved my "INDIA" in which these comuter logicmind are
living.........
Is This Answer Correct ? | 2 Yes | 5 No |
You want to link a c++ program to c functions. How would you do it?
Write syntax to define friend functions in C++.
How does a C++ structure differ from a C++ class?
What is #include iostream h in c++?
Write a single instruction that will store an EVEN random integer between 54 and 212 inclusive in the variable myran. (NOTE only generate EVEN random numbers)
What is a friend function in c++?
What is lazy initialization in c++?
Can we use resume in error handling i.e. in the catch block
Explain what are the sizes and ranges of the basic c++ data types?
Explain stack unwinding.
What do you mean by Stack unwinding?
What will happen if when say delete this ?