Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python: This program will determine if the number entered by the user is happy or not happy. This process involves computing the sum of the
Python: This program will determine if the number entered by the user is happy or not happy. This process involves computing the sum of the squares of the digits of a number. If the number n is not initially or n is updated to be the sum of the squares of its digits. This process is then repeated until n either n reaches happy or not happy This process is explained further below. Your program should include the following functions:
sumofsquares This function has one parameter of type int and returns the sum of the squares an integer This function can assume the argument is a nonnegative integer. To calculate the sum of squares, square each digit individually then sum all the squares. For example, sumofsquares returns since To implement the sum of squares, use the and operators. Remember that for any integer n greater than zero, n will result in the digit in the one's place, and that n will result in all the digits except for the digit in the one's place. For example, is and is An alternative approach is to convert the parameter to a string, then iterate through the characters digits of the string, converting each digits back to a number and squaring it
happy This function has one parameter of type int and returns a boolean value representing if the parameter number is happy or not happy. For example, happy returns False. To determine if a number is happy or not happy, repeatedly find the sum of the squares until either or is reached. If is reached, the number is happy, and if is reached, the number is not happy. Each time you call the sumofsquares function, be sure to print the result to the screen. For example, happy returns True since and then and the function call happy would print the following also see the sample run above:
Note that if is reached, then the number is stuck in a circle: perhaps we should call these numbers dizzy numbers!
main This function has no parameters and does not return a value. This function is the main driver of the program. It should first print the welcome message. Then it should ask the user to input a number, call the happy function, and based on the returned value, print either:
# is NOT a happy number. :
or
# is a happy number. :
where # is the number input by the user.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started