Hello,
I have just a simple and basic question. Write a program in c++ to swap 2 numbers without using a third variable.
:D

Hello,
I have just a simple and basic question. Write a program in c++ to swap 2 numbers without using a third variable.
:D

just focus on what u can do and what u can not.

u can not use a third variable.
wat u can do is "4 operations of + _ * and /". by using these operations u have to do it.
a hint is

if a = 5 and b = 9
u have to make b = 5 and a = 9.
to make b = 5 u have to subtract 4 from 9. but where wil that 4 comes from? now its your turn.................

just focus on what u can do and what u can not.

u can not use a third variable.
wat u can do is "4 operations of + _ * and /". by using these operations u have to do it.
a hint is

if a = 5 and b = 9
u have to make b = 5 and a = 9.
to make b = 5 u have to subtract 4 from 9. but where wil that 4 comes from? now its your turn.................

Ok fine, here is the solution:-
suppose a=10, b=5
a=a+b; //a=15
b=a-b; //b=10
a=a-b; //a=5
It needs only 3 steps .

commented: No! Don't give something for nothing! +0
commented: read the forum rules, don't just give out the answer, teach, make them think +0

Ok fine, here is the solution:-
suppose a=10, b=5
a=a+b; //a=15
b=a-b; //b=10
a=a-b; //a=5
It needs only 3 steps .

You should not tell him the solution. That is wrong

You should not tell him the solution. That is wrong

I'm guilty of the mistake too, but that is the OP posting what they came up with after your comments. Their wording could have been much less snooty though.

The following code will do it...

#include<iostream>
using namespace std;
int main()
{
	int a=5,b=10;
    cout << "a: " << a << " b: " << b <<endl;
	a=a+b;
	b=a-b;  //this makes b=a
	a=a-b;  //this makes a=b
	cout << "a: " << a << " b: " << b <<endl;
}

> Write a program in c++ to swap 2 numbers without using a third variable.
The short answer is you can't, at least not in any way that is remotely useful.

For those suggesting addition, try this

#include <stdio.h>
#include <limits.h>

void f1 ( void ) {
  double a = 1E10, b = 1E-10;
  printf( "a=%.10f, b=%.10f\n", a, b );
  a = a + b;
  b = a - b;
  a = a - b;
  printf( "a=%.10f, b=%.10f\n", a, b );
  printf( "Oh dear, the tiny value got lost next to the huge value\n\n" );
}
void f2 ( void ) {
  int a = INT_MAX, b = INT_MAX - 1;
  printf( "%d %d\n", a, b );
  a = a + b;
  printf( "a is now %d - overflow\n", a );
  b = a - b;
  a = a - b;
  printf( "%d %d\n", a, b );
}

int main ( ) {
  f1();
  f2();
  return 0;
}

$ gcc foo.c
$ ./a.out 
a=10000000000.0000000000, b=0.0000000001
a=0.0000000000, b=10000000000.0000000000
Oh dear, the tiny value got lost next to the huge value

2147483647 2147483646
a is now -3 - overflow
2147483646 2147483647

So what you say, at least int eventually gave the right answer.
True, but only by luck rather than skill.

H.2.2 Integer types
1 The signed C integer types int, long int, long long int, and the corresponding
unsigned types are compatible with LIA−1. If an implementation adds support for the
LIA−1 exceptional values integer_overflow and undefined, then those types are LIA−1
conformant types. C’s unsigned integer types are ‘‘modulo’’ in the LIA−1 sense in that
overflows or out-of-bounds results silently wrap. An implementation that defines signed
integer types as also being modulo need not detect integer overflow, in which case, only
integer divide-by-zero need be detected.

The implementation is within its rights to cause a trap on integer overflow. So rather than swapping two variables, your machine is instead rebooting.

You might want to ask yourself why your tutor is teaching you stupid assembler programming tricks from the 1970's.

Here's why you should write this:

temp = a;
a = b;
b = temp;

- it ALWAYS works, irrespective to the type. Yes, you can even use it on structs or classes if you want. Try that with your dumb math tricks.
- there is no danger of a "loss of precision" (good for swapping floats).
- there is no danger of an arithmetic overflow (good for integers of all types)
- the compiler probably going to see it as a "swap", and may end up doing some thing you didn't think of doing. For example, generating an actual "exch" instruction. The obfuscated code is just as likely to confuse a compiler as it is to confuse the average reader.

commented: Well said. +13
commented: Detailed +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.