Description
A self number*
is a positive integer which cannot be generated by any other integer added to the
sum of that other integer’s digits. For example, 4671 is not a self number, because it can be
generated by the sum of 4653 and the digits 4, 6, 5, and 3. That is, 4671 = 4653 + 3 + 5 + 6 + 4.
Note that a non-self number can have more than one such summation. For example, the non-self
number 113 has both 113 = 106 + 6 + 0 + 1 and 113 = 97 + 7 + 9. On the other hand, 4674 is a
self number, as no such sum will generate the integer 4674.
In this assignment, you will write a program, with nested loops, to obtain an user input 𝑛, and find
the smallest self number 𝑥 where 𝑥 ≥ 𝑛. To do so, you iterate 𝑥 = 𝑛, 𝑛 + 1, 𝑛 + 2, 𝑛 + 3, … For each
𝑥, you check all integers 𝑘 in the range [1 … 𝑥] to see whether 𝑥 equals the sum of 𝑘 and the digits
of 𝑘. If there exists such an integer 𝑘, then 𝑥 is not a self number. If no such integer 𝑘 exists, then 𝑥
is a self number. The iteration stops when a self number is found.
Program Specification
The program will prompt the user to enter an input number. You can assume that the input
number is always an integer, but may not be positive.
When the input is not positive, you should repeatedly display a warning and prompt the user to
make another input until the input is positive.
Iterating from the user input and up, the program will then print whether it finds a self number or
not. If a number is not a self number, you have to print the summation that yields the number. In
the summation, you should print out the largest found integer and its digits from rightmost to
leftmost that yields the number. For the example 4671, you should print out 4671 = 4653 + 3
+ 5 + 6 + 4. For the example 113, you should print out 113 = 106 + 6 + 0 + 1 (but not
113 = 97 + 7 + 9).
On the other hand, if a self number is found, the program prints a message and can terminate.
You can see the exact printing format in the next section.
Program Output
The following shows some sample output of the program. The blue text is user input and the other
text is the program output. You can try the provided sample program for other input. Your program
output should be exactly the same as the sample program (i.e., same text, same symbols, same letter
case, same number of spaces, etc.). Otherwise, it will be considered as wrong, even if you have
computed the correct result.
*
Self number, https://en.wikipedia.org/wiki/Self_number (retrieved 17 Sep 2018).
Enter an integer: 4671↵
4671 = 4653 + 3 + 5 + 6 + 4
4672 = 4649 + 9 + 4 + 6 + 4
4673 = 4654 + 4 + 5 + 6 + 4
4674 is a self number.
Enter an integer: -789↵
Invalid. Try again!
Enter an integer: 692↵
692 is a self number.
Enter an integer: 0↵
Invalid. Try again!
Enter an integer: -11↵
Invalid. Try again!
Enter an integer: 112↵
112 = 110 + 0 + 1 + 1
113 = 106 + 6 + 0 + 1
114 = 111 + 1 + 1 + 1
115 = 107 + 7 + 0 + 1
116 = 112 + 2 + 1 + 1
117 = 108 + 8 + 0 + 1
118 = 113 + 3 + 1 + 1
119 = 109 + 9 + 0 + 1
120 = 114 + 4 + 1 + 1
121 is a self number.
Enter an integer: 12345↵
12345 = 12333 + 3 + 3 + 3 + 2 + 1
12346 = 12329 + 9 + 2 + 3 + 2 + 1
12347 = 12334 + 4 + 3 + 3 + 2 + 1
12348 is a self number.
Submission and Marking
Your program file name should be selfnum.cpp. Submit the file in Blackboard
(https://blackboard.cuhk.edu.hk/).
Insert your name, student ID, and e-mail as comments at the beginning of your source file.
You can submit your assignment multiple times. Only the latest submission counts.
Your program should be free of compilation errors and warnings.
Your program should include suitable comments as documentation.
Plagiarism is strictly monitored and heavily punished if proven. Lending your work to others is
subjected to the same penalty as the copier.