swapping program does not use third variable

Answers were Sorted based on User's Feedback



swapping program does not use third variable..

Answer / jayabalan.s

void main()
{
int a,b;//example a=10,b=12;
a=a+b;//a=22
b=a-b;//b=10;
a=a-b;//a=12
}

Is This Answer Correct ?    17 Yes 0 No

swapping program does not use third variable..

Answer / jayasrinivas.donavalli

class swap
{
public:
int a,b;
void swapp()
{
a = a + b;
b = a - b;
a = a - b;
}
};
class swap s1.
s1.swapp();
cout<<a<<b;

Is This Answer Correct ?    9 Yes 3 No

swapping program does not use third variable..

Answer / venkanna

swap(a,b);
{
a=a+b;
b=a-b;
a=a-b;
}
Explenation:
if a=5 and b=10
a=a+b(5+10)=15
b=a-b(15-10)=5------------swaped here
a=a-b(15-5)=10------------

Is This Answer Correct ?    6 Yes 2 No

swapping program does not use third variable..

Answer / abdul rahman

void main()
{
int a,b;//example a=10,b=12;
a=a~b;//a=22
b=a~b;//b=10;
a=a~b;//a=12
}


NOTE:
~=XOR operator( not able to write xor
operator so iam using this symbol which is not
correct)

Is This Answer Correct ?    4 Yes 2 No

swapping program does not use third variable..

Answer / rajeshwar raja

Swap(int* a, int* b)
{
(*a) ^= (*b) ^= (*a) ^= (*b);
}

Explanation:
Expressions are evaluated from right to left.
Take the right most operation (*a) ^= (*b), its is short
hand XOR operation between 'a' and 'b'.
Assume a = 2 and b = 3.
a = 2 ^ 3 = 1
Next operation (*b) ^= (*a),
b = 3 ^ 1 = 2 (Note a is 1 now) ***(b is 2)***
Next operation (*a) ^= (*b)
a = 1 ^ 2 = 3 (Note b is 2 now) ***(a is 3)***

SWAPPED!!!
It can swap complex data structures also.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More OOPS Interview Questions

what is cast operator?

2 Answers   Microsoft,


create a c++ program that will ask 10 numbers and display their sum using array.

1 Answers  


How is class defined?

0 Answers  


How do you use inheritance in unity?

0 Answers  


Which is better struts or spring?

0 Answers  






inheritence with example

1 Answers  


what is the main difference between sizeof() operator in c and c++

3 Answers  


write a program to find the largest of two numbers without using for,while,switch,if else, conditional operator and do while using c++ pgmng language

3 Answers   Satyam,


Please tell me the oops concept with detailed answer

9 Answers   EEE,


write a program that takes input in digits and display the result in words from 1 to 1000

0 Answers   Wipro,


#include <iostream> using namespace std; int main() { int a = 3; int c[5][5]; for (int x=0;x<5;x++) { for (int y=0;y<5;y++) { c[x][y] = x*y; } } cout << c[a][2]; }

1 Answers  


why the memory allocated with new cant be freed using free()

2 Answers  


Categories