Write a program that determines which numbers produce a


Mathematically, given a number , we can define a sequence , , ... where is the sum of the squares of the digits of . nis happy if and only if there exists isuch that .

Here is an example:

First, take each digit of any positive integer, square them and add them together. We will use the number nineteen as an example:

19 → 12 + 92 = 82

Then take each digit of this number, square them and add them together:

82 → 82 + 22 = 68

And repeat until the result is 1:

68 → 62 + 82 = 100

100 → 12 + 02 + 02 = 1

So when the sum of the squares of the digits in the last member in the sequence starting with neventually reduces to 1, is happy.

Additionally:

If a number is happy, then all members of its sequence are happy. If a number is unhappy, all members of its sequence are unhappy.

The above example sequence: 19, 82, 68, 100 is happy, and so all of the terms within the sequence are also happy.

Write a program that determines which numbers produce a happy sequence, within a series of test numbers.

The user will provide a first number to test and a last number to test. The program will test the numbers starting with the first number, incrementing by one, and repeating until the last number.

Program Implementation Requirements:

1. Tell the user what the program does before prompting for input.

2. mainwill call a user-defined function to read the first and last numbers for the range to test from the user.

- Theuser-defined function will:

o Separately read and validate each number. Both the first and last numbers must be at least 1 and can be up to 9999 (remember to define constants for these values!)

If the first number is not in the correct range, issue an error message and then re-prompt until the number entered is valid.

oThe last number cannot be smaller than the first number.

If the last number is not in the correct range or is smaller than the first number, issue an error message and then re-prompt until the number entered is valid.

o Pass back both values to main using reference parameters.

3. mainwill start with the first number as the test number, and loop until all numbers including the last number have been tested. Each time the loop runs, main will:

o Call a user-defined function to test if the number is happy.

o Display the results (whether the sequence is happy or unhappy), and pause so the results can be read by the user.

o Increment the test number.

- Theuser-defined function to test whether a number is happy must implement nested loops, as follows:

o A test number will be passed into the function.

o The function's outer loop will test the number, as follows:

o Use an inner loop to compute the sum of the squares of the digits in the current term. NOTE: You must use a loop, so this program will still work for larger numbers, if the maximum value of the last number constant is modified.

o Count and display each term.

o Each term displayed should be formatted to a width of 8, and 10 terms should be displayed per line.

o Repeat with the sum of the squares of the digits in the current term becoming the next term to test, until it is determined:

The test number is happy (when the sum of the square of the digits is 1)

The test number is unhappy (when 50 terms have been computed, without reaching 1).

o After the outer loop exits, the function shall display the number of terms in the sequence.

o A booleanvalue will be returned to main, indicating whether the displayed sequence is happy or not.

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Write a program that determines which numbers produce a
Reference No:- TGS01086818

Expected delivery within 24 Hours