CSCI1540 Fundamental Computing with C++

CSCI1540 Fundamental Computing with C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong KongPage 1 of 2 Assignment 3: K

Due: 23:59, Thu 17 Oct 2024Full marks: 100

Introduction

The objective of this assignment is to master the use of selective and nested repetitive control flow.You will write nested loops to look for Kaprekar numbers.A Kaprekar number is a positive integer 𝑥 if the digits of the square 𝑥 2 can be split into two partsthat add up to 𝑥, where the part formed from the low-order (rightmost) digits of 𝑥 2 must be nonzero (although leading zeroes are allowed in the part). For example, 45 is a Kaprekar number,because 452 = 2025 can be split into two parts 20 and 25, with 20 + 25 = 45. As another example,999 is also a Kaprekar number, because 9992 = 998001 and 998 + 001 = 999. (Note the loworder part 001 includes leading zeroes.) However, 31 is not a Kaprekar number, because all threepossible splits of 312 = 961 are no good (96 + 1 ≠ 31, 9 + 61 ≠ 31, and 0 + 961 ≠ 31).

Program Specification

The program shall ask for two user inputs 𝑠 and 𝑛. They are assumed to be always integers. You arerequired to use the data type long long instead of int for all integer variables in this assignment,because a square can easily go overflow with (that is, larger than the upper bound of) the int type.

  1. The program first prompts the user to enter an integer 𝑠. When 𝑠 is not positive, you shalldisplay a warning message and ask for another input, until 𝑠 is positive. Your program shall lookfor Kaprekar number(s) greater than or equal to 𝑠.
  1. Then prompt the user to enter how many Kaprekar numbers the user is looking for. When thisinput 𝑛 is not positive, display a warning message and ask for another input, until 𝑛 is positive.
  1. After validating inputs 𝑠 and 𝑛, repeatedly test 𝑠, 𝑠 + 1, 𝑠 + 2, 𝑠 + 3, 代 写CSCI1540 Fundamental Computing with C++ … to check whether it is aKaprekar number. If so, print out the square and the split of the two parts. (See the upcomingSample Runs section for the printing format.) Repeat these checks until 𝑛 Kaprekar numbers arefound and printed. Then program execution ends.

➢ To check whether a number 𝑘 is a Kaprekar number, you progressively check all possiblesplits of 𝑘 2 . E.g., let 𝑘 = 345 and hence 𝑘 2 = 119025. The first split to check is 11902 and

5; the next split is 1190 and 25; the next split is 119 and 025; and so on.Note: In this assignment, you are not allowed to: (1) use any functions in the <cmath> library, (2)write any functions other than main(), and (3) use any arrays/vectors (which are not yet taught).

Sample Runs

In the following sample runs, the blue text is user input and the other text is the program printout.ou can try the provided sample program for other input. Your program output should be exactly the same as the sample program (same text, symbols, letter case, spacings, etc.). Note that there is a space after the ‘:’ and ‘?’ n the program printout.CSCI1540 Fundamental Computing with C++, Fall 2024/25Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright © 2024 CSE, CUHKPage 2 of 

Enter an integer: 40↵

How many Kaprekar numbers? 3↵

45^2 = 2025

20 + 25 = 45

55^2 = 3025

30 + 25 = 55

99^2 = 9801

98 + 1 = 99

Enter an integer: -23↵

Input must be +ve! Enter again.

Enter an integer: -1↵

Input must be +ve! Enter again.

Enter an integer: 0↵

Input must be +ve! Enter again.

Enter an integer: 200↵

How many Kaprekar numbers? 0↵

Input must be +ve! Enter again.

How many Kaprekar numbers? 0↵

Input must be +ve! Enter again.

How many Kaprekar numbers? -777↵

Input must be +ve! Enter again.

How many Kaprekar numbers? 5↵

297^2 = 88209

88 + 209 = 297

703^2 = 494209

494 + 209 = 703

999^2 = 998001

998 + 1 = 999

2223^2 = 4941729

494 + 1729 = 2223

2728^2 = 7441984

744 + 1984 = 2728

Submission and Marking

  • Your program file name shall be kaprekar.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.
  • Do NOT share your work to others and do NOT plagiarize. Both senders and plagiarists shall bepenalized.
posted @ 2024-10-15 11:07  r78b6d  阅读(20)  评论(0)    收藏  举报