CSCI1120 Assignment 2: Self Number

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment

Description

5/5 - (3 votes)

Introduction

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, http://en.wikipedia.org/wiki/Self_number (retrieved 17 Sep 2018).

CSCI1120 Introduction to Computing Using C++
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.