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?