UVA-10591 Happy Number

Source: Internet
Author: User

Happy number

UVA-10591

Let the sum of the square of the digits of a positive integer S0 is represented by S1. In a similar-a-to, let the sum of the squares of the digits of S1 is represented by S2 and so on. If Si = 1 for some i≥1 and then the original integer S0 are said to be Happy number. A number, which is isn't happy, is called unhappy number. For example 7 is a Happy number since 7→49→97→130→10→1 and 4 are an unhappy number since 4→16→37→58→89→ 145→42→20→4.

Input

The input consists of several test cases, the number of which you is given in the first line of the input. Each test case consists the one line containing a single positive integer N smaller than 109.

Output

For each test case, you must print one of the following messages:

Case #p: N is a Happy number.

Case #p: N was an unhappy number.

Here P stands is the case number (starting from 1).

You should print the first message if the number N is a happy number. Otherwise, print the second line.

Sample Input

3

7

4

13

Sample Output

Case #1:7 is a Happy number.

Case #2:4 are an unhappy number.

Case #3: Happy number.

The problem of recursion or cycle I feel very good, to tell the truth first time I did not understand the problem, is to give you a number, if the number of each number of square sum is 1 is happy, manually tried, less than 10 of only 1 satisfied, so I just put it squared and to a number on the line, Otherwise the loop will not go out, and then we can continue the loop recursion.

#include <stdio.h>intFintb) {    ints=0;  while(b) {intt=b%Ten; S+=t*T; b/=Ten; }    if(s<Ten)returns; Else returnf (s);}intMain () {intT; scanf ("%d",&T);  for(intI=1; i<=t; i++)    {        intx; scanf ("%d",&x); printf ("Case #%d:%d is a", i,x); if(f (x) = =1) printf ("Happy number.\n"); Elseprintf"N Unhappy number.\n"); }    return 0;}

UVA-10591 Happy Number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.