Swap 2 numbers without temp variable! Please help!

I am working on an assignment and I need help with swapping 2 numbers without the use of a temporary variable and for it to have cout and cin. I need the number to be typed in using cin and the swapped number displayed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main()
{
 // 1: Swap 2 numbers without using a temporary variable and display:
 {
    var a = 15, b = 30;
    a = a - b;
    a = a + b;
    b = a - b;
    
    
    
    
    
    
    return (0);
 }
There's something called std::swap() try it :D
Haha, I wish I could. This is the format my program has to be in for right now. My professor won't let us use code what we haven't learned in class. Basically this is what my program has to look like, but I need it to be able to output a result.
blasphemy
Last edited on
I know. I'm pretty confused right now as to what I should do.
Try the following example, it should work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main()
{
 // 1: Swap 2 numbers without using a temporary variable and display:
 
    int a = 15, b = 30;
    a = a + b;
    b = a - b;
    a = a - b;
    
    
    
    
    
    
    return (0);
 }
Topic archived. No new replies allowed.