Responsive Ads Here

Monday, November 24, 2014

UVA - 10591 - Happy Number

10591 - Happy Number

Problem C

Happy Number

Time Limit

1 Second

Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the squares of the digits of S1 be represented by S2 and so on. If Si = 1 for some i ³ 1, then the original integer S0 is said to be Happy number. A number, which is not happy, is called Unhappy number. For example 7 is a Happy number since 7 -> 49 -> 97 -> 130 -> 10 -> 1 and 4 is 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 are given in the first line of the input. Each test case consists of 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 is an Unhappy number.

Here p stands for 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

Output for Sample Input

3

7

4

13

Case #1: 7 is a Happy number.

Case #2: 4 is an Unhappy number.

Case #3: 13 is a Happy number.

Problemsetter: Mohammed Shamsul Alam

International Islamic University Chittagong

Special thanks to Muhammad Abul Hasan

#include<stdio.h>
find(int n)
{
    int i,k,p=0,l;
    for(i=n; i!=0; i=i/10)
    {
        k=i%10;
        p=p+k*k;
 
    }
    if(p>9)
        return find(p);
    else
        return p;
}
int main()
{
    int n,nn,k,c,i,R,T=1;
    scanf("%d",&R);
    while(R>0)
    {
        scanf("%d",&n);
        if(n==0){
            printf("Case #%d: %d is an Unhappy number.\n",T,n);
                }
        else if(n==1)
            printf("Case #%d: %d is a Happy number.\n",T,n);
        else if(n==2)
        printf("Case #%d: %d is an Unhappy number.\n",T,n);
        else if(n==3)
            printf("Case #%d: %d is an Unhappy number.\n",T,n);
       else if(n<10)
        {
            nn=n*n;
            for(i=nn; i>10; i=c)
            {
                c =find(nn);
                if(c==1)
                    printf("Case #%d: %d is a Happy number.\n",T,n);
                else
                    printf("Case #%d: %d is an Unhappy number.\n",T,n);
            }
 
        }
        else
        {
            int  c=find(n);
            if(c==1)
                printf("Case #%d: %d is a Happy number.\n",T,n);
            else
                printf("Case #%d: %d is an Unhappy number.\n",T,n);
        }
        T++;
        R--;
    }
    return 0;
}

10591 - Happy Number uva link

No comments:

Post a Comment

php4

<?php    // Start the session  session_start();  ?>  <!DOCTYPE html>  <html>  <head>       <link rel=&q...