Assignment 3: Self Number

$24.99 $18.99

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 =…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 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.

CSCI1120 Introduction to Computing Using C++, Fall 2018-19

Department of Computer Science and Engineering, The Chinese University of Hong Kong

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

  • 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.

Assignment 3: Self Number
$24.99 $18.99