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

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

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