1089 magic trick

I am trying to write a program for the 1089 trick.
What it do is users can input any three digits number which cannot contain repeated number eg(333,122,555,455,688,etc.) <<these are not allowed.

The program will take this three digits number,
1. reverse it
2. subtracting the reversed number by the original number and have the "result 1".
3. reverse the "result 1" you got "second reversed number".
4. adding the "second reversed number" to "result 1".
5 output should have 1089

The original trick detail will be shown in the Youtube video https://www.youtube.com/watch?v=ADMsA5qa0Uw


And I have problem with the subtracting part

here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

int n;
int reversedNumber = 0;

int intro()
{		
	cout << "Welcome to the number guessing game! \nIf you concentrate, sometimes you can connect to the electrons in the computer!" << endl;
	cout << "Let's try it. Think of a three digit number. (To make it harder, make the digits \nall different from each other). Type in your number: "; cin >> n;
	cout << endl;
	cout << "I'll help you with the math.  Lets randomize those digits by reversing them, and do a subtraction: " << endl << "  " << n << "  (The original number)" << endl;
	
	return n;
}

int reverse()
{
	int remainder;	
	while (n != 0)
	{
		remainder = n % 10;
		reversedNumber = reversedNumber * 10 + remainder;
		n /= 10;
	}
	cout << "- " << reversedNumber << "  (The reversed digits)" << endl;
	cout << "=====" << endl;
	return reversedNumber;	
	
}

int subtraction()
{
	int SubtractResult;
	SubtractResult = n-reversedNumber;
	cout << " " << SubtractResult << endl;
	return 0;
}


 int main()
{	
	intro();
	reverse();
	subtraction();	
	system("pause");
	return 0;
}







my subtraction output always equal to the negative reversedNumber which shouldn't happen.
Anyone can help with this?


Last edited on
Its because your variable n will always equal to 0 after you run reverse(); function, so in line 35 you will always be subtracting zero with reversedNumber.
I hate to have to tell you @kevinchan246 ... but that algorithm won't always produce 1089.
Watch the video all the way through. (You are also supposed to take the absolute difference in the subtraction - trying to reverse a negative number wouldn't make sense).

There are 648 legitimate 3-digit numbers with no repeated digits and 136 of them produce 198 rather than 1089. Any 3-digit number of the forms
a b (a+1)
or
(a+1) b a
will produce 198, not 1089.

An alternative statement of the problem could have the additional caveat
Write down a three-digit number whose digits are distinct and decreasing.
which will rule out both of those pathological cases above.
Last edited on
The description indicates that you reverse two numbers, but you only call reverse() once. Also, reverse() assumes that n==0 and reversedNumber==0 upon entry.

I suggest that you change it to be a function:
1
2
3
4
5
// return num with its digits reversed. Num must be positive
int reverse(int num)
{
    ...
}

I'd also do all the output in main(). Functions that compute things should just compute. They shouldn't output.
Thank you all!
I have read through all your replies and I will try your advises !!


BTW @lastchance thank you for pointing out the issue it is really nice to know this!
Last edited on
Topic archived. No new replies allowed.