Determines which numbers produce a happy sequence

Assignment Help Programming Languages
Reference no: EM13806569

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.

Reference no: EM13806569

Questions Cloud

Why is staffing important : Why is staffing important
Create a graphic that includes a business logo : You will need to create a graphic that includes a business logo and a dollar amount of current sales. Your logo must include at least 1 shape, defined by a geometric path, using class GeneralPath, and the name of your business
What is the purpose of performance appraisals : What is the purpose of performance appraisals
What do you think about the concept of alcohol abuse : What do you think about the concept of alcohol abuse
Determines which numbers produce a happy sequence : Write a program that determines which numbers produce a happy sequence, within a series of test numbers
How would the intensity of light vary : How would the intensity of light vary with distance from the source according to the (a) wave theory, (b) particle (photon) theory? How can these two theories mesh?
Does the electron gain or lose potential energy : An electron is accelerated by a uniform electric field (1000 V/m) pointing vertically upward. Use energy methods to get the magnitude and direction of its velocity after it moves 0.10 cm form rest. Does the electron gain or lose potential energ..
Write a research about william dawson and john phillip sousa : Write a research about those 3 people William Dawson, Florence and John Phillip Sousa. Make it three separate research and each person you have to write a 3 pages research about him or here.
A survey of probability concepts issues : Create a variable that shows the teams that play in a stadium less than 15 years old versus one that is 15 years old or more.

Reviews

Write a Review

Programming Languages Questions & Answers

  Create object-oriented design to enter name into text box

Create the object-oriented design for the system which permits the user to enter their name into the text box. When they press Go button message.

  Write class which uses string array or arraylist

Write the class which uses string array or arraylist object to holld five students, array to hold five charters student letter grade) and five arrays of doubles each to hold each students set.

  Create logic for program which contains housekeeping

Create logic for program in pseudocode or flowchart which contains housekeeping, detail loop, and end-of-job modules, and which computes service charge and the original check amount customers owe for writing bad check.

  Create classes essential to create service quote

Create classes essential to create service quote. Write the application program which will use classes to create the objects and output the service quote.

  Console application for a furniture company

Write a C# console application for a furniture company.  Ask the user to choose P for pine, O for oak, and M for mahogany. Show the price of a table manufactured with the chosen wood.

  Problem 1you have gathered the following information for a

problem 1you have gathered the following information for a friend who manages a restaurant. you will use this data for

  Building instruction set simulators

Building Instruction Set Simulators

  Write function to calculate and print fractional powers

Write down a function which calculates and prints fractional powers of its first argument as shown below for first argument of 2(1/2,1/4,1/8 and so on).

  Deck of cards programming class

Write a complete program using "ECLIPS" that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two..

  Program to compute person-s body mass index

Write a program to compute person's body mass index? Create a modular program which calculates and display person's body mass index (BMI).

  Arithmetic and application of summaries

Write some R code that carries out computation, but do it with a single R expression that contains no commas (,) and no braces ({}).

  Prepare a function that generates a polynomial

Write a function genPoly(n) that generates a polynomial of length n (degree n-1), all of whose coefficients are 1.0.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd