Question

2 Happy Numbers 2.1 Definition The definition of a happy number, from wikipedia Given a number n = no, define a sequence n1, n2, … where ni+1 1S the sum of the squares of the digits of n,. Then n is happy if and only if there exists i such that ni 1 It can be shown (see wikipedia) that n is unhappy if the number 4 appears in the sequence. 2.2 The program Write a C++ program happy.cpp which reads numbers, one at a time, from the terminal input, and outputs x is happy or x is not happy For example: > happy 4 is not happy 1 is happy 1000 1000 is happy Your program should handle the calculation of happiness for all numbers betweern 1 and 2147483647 Your program should continue answering about the happiness of numbers until 0 is entered, at which time the program should exit silently

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

int happySum(int n){

int sum = 0;

while(n > 9){

sum = 0;

while(n!=0){

int r = n % 10;

sum = sum + r * r;

n = n/10;

}

n=sum;

}

return n;

}

int isHappy(int n){

n = happySum(n);

if( n == 1 || n == 4){

return 1;

}

return 0;

}

int main()

{

int i;

while(1) {

cout<<"Enter a number: ";

cin>>i;

if(i==0)

break;

if(isHappy(i)){

cout<<i<<" is Happy"<<endl;

}else{

cout<<i<<" is not Happy"<<endl;

}

}

return 0;

}


============================
See Output


Thanks, PLEASE UPVOTE if helpful

Add a comment
Know the answer?
Add Answer to:
2 Happy Numbers 2.1 Definition The definition of a happy number, from wikipedia Given a number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program divisorsum.cpp which which reads numbers, one at a time, from the terminal...

    Write a C++ program divisorsum.cpp which which reads numbers, one at a time, from the terminal input, and outputs the proper divisors and the proper divisor sum for that integer in an equation format. Here is an example: > divisorsum 6 6: 1+2+3 = 6 888 888: 1+2+3+4+6+8+12+24+37+74+111+148+222+296+444 = 1392 0 > Note: spaces will be ignored, but punctuation symbols (:+=) must be in the correct places. Your program should handle the calculation of proper divisor sums for all numbers...

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...

    I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...

  • Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go wrong Consider...

    Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go wrong Consider how to gracefully shut down the program and save the users data and close files properly. Typically there are two scenarios to make a distinction between: The first is one that which you have absolutely no control over; such as if all the files are where they should be, and that the user has not deleted one or accidentally moved it rather than copied...

  • CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

    CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The Assignment ? Specifications General Structure your file name and class name on the following pattern: The first three letters of your last name (begin with upper case.). Then the first two letters of your first name (begin with upper case.). Follow this with the name of the program: TryCatch. For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid...

    /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, BYE Enter BYE (case insensitive) to exit the program. ****************************************************/ import java.util.Scanner; public class HexUtilitySOLUTION { public static void main(String[] args) { // Maximum length of input string final byte INPUT_LENGTH = 4; String userInput = ""; // Initialize to null string Scanner input = new Scanner(System.in); // Process the inputs until BYE is entered do {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT