Magic Number

So something isn't working right in this 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
 cout << "Enter a 3 digit number, whose first digit is greater than its last\n";
		cin >> number1;
		cout << "The number you entered is:" << number1<<endl;
		
		while (number1 > 0)
		{
			number2 = number2 * 10 + (number1 % 10);
			number1 = number1 / 10;
			cout << "The reverse of your input is " << number2<<endl;
			
			
		}
		number3=(number1-number2);
		cout << "Subtraction of the reversal from the original number is " << number3<<endl;
		

		{
			number4 = number4 * 10 + (number1 % 10);
			number3 = number3 / 10;
			cout << "The reverse of the resulting number is " << number4<<endl;

		}
		number5 = (number3 + number4);
		cout << "Addition of the number to the un-reversed form is " << number5 << " our magic number" << endl;


It should be taking a number, reversing it, subtracting the reversal from the original, reversing it again and then adding the number to the un-reversed form. Like this 901 109 901-109=792 297+792=1089.

When I run it, the results look like this


Enter a 3 digit number, whose first digit is greater than its last
801
The number you entered is:801
The reverse of your input is 1
The reverse of your input is 10
The reverse of your input is 108
Subtraction of the reversal from the original number is -108
The reverse of the resulting number is 0
Addition of the number to the un-reversed form is -10 our magic number
Press any key to continue . . .

You didn't keep your original number.

You saved the input to 'number1', and then you changed it to 0 during reversing. that's why you get -108.
I don't get your meaning
He means that by the time you reach line 13, number1 is zero. This is because you keep dividing it by 10 at line 8. For the same reason, by the time you reach line 23, number3 will also be zero.

This would be a whole lot easier if you created a function to reverse a number:
int reverse(int num);Then the main algorithm would be
1
2
3
4
5
6
7
8
number2 = reverse(number1);
cout << "The reverse of your input is " << number2<<endl;
number3 = number1 - number2;
cout << "Subtraction of the reversal from the original number is " << number3<<endl;
number4 = reverse(number3);
cout << "The reverse of the resulting number is " << number4<<endl;
number5 = number3+number4;
cout << "Addition of the number to the un-reversed form is " << number5 << " our magic number" << endl
Topic archived. No new replies allowed.