Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 2. (50 points) Happy Numbers A happy number is a number defined by the following process: Starting with any positive integer, replace the number

image text in transcribed

image text in transcribed

Exercise 2. (50 points) Happy Numbers A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers. For example, 13 is a happy number since 12+32=1+9=1012+02=1+0=1 And 85 is an unhappy number since 82+52=64+25=8982+92=64+81=14512+42+52=1+16+25=4242+22=16+4=2022+02=4+0=442=1612+62=1+36=3732+72=9+49=5852+82=25+64=89,whichisarepeatedresult. It can be shown that if a number is an unhappy number, it will reach 4 in the above process. This can be used to check whether a number is an unhappy number. Our job is to write code to check whether a positive integer is a happy number. Part of the code is already given. As shown below, we only need to fill in the code under TODO. Note to print out the intermediate numbers to match the expected output. if ( n=1) printf( 1%d is a happy number. ",m); else printf("\%d is a NOT a happy number. ",m); return 0 ; \} Below are a few example runs. $./ happy n=10 1 10 is a happy number. $./ happy n=101 2 4 101 is a NOT a happy number. $./ happy n=111 3 9 81 65 61 37 58 89 145 42 20 4 111 is nOT a happy number. $./ happy n=888 192 86 100 1 888 is a happy number

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

1583473858, 978-1583473856

More Books

Students also viewed these Databases questions

Question

explain what is meant by redundancy

Answered: 1 week ago