Lucky number – Facebook

                                                 Lucky Number

                                     

Nandhini is an active user of FB and she keeps updating all information about her protests and the social evils she is fighting against in her FB page.  She used to get many likes for all her posts.
     
FB gives some gift vouchers to few users everyday. The lucky users are selected based on the number of likes that a post has obtained on a particular day. If the number of likes obtained for a post is a Lucky Number, the FB user receives a Gift Voucher. 

        


A Lucky 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 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 lucky numbers.
Example:    19 is a lucky number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
1+ 02 + 02 = 1

Write a program to find whether Nandhini would receive the gift voucher on a particular day. Input is the number of likes that her post received on a particular day.

Business Rules:
Print Invalid Input if the input is negative or if the input is greater than 1000000.
Input Format:
Input consists of an integer that corresponds to the number of likes.
Output Format:
Output consists of a string that is either “YES” or “NO” or “Invalid Input”.

Sample Input 1:
19
Sample Output 1:
YES

Sample Input 2:
20
Sample Output 2:
NO

Sample Input 3:
-20
Sample Output 3:
Invalid Input
 


Code:

#include<stdio.h>
int main()
{
    int n,t,s=0;
    scanf(“%d”,&n);
    if(n<0||n>1000000)
        printf(“Invalid Input”);
            else
                {
                    s=n;
               while(s>9)
               {
                  
                    s=0;
                    while(n>0)
                    {
                      t=n%10;
                        s+=t*t;
                       n/=10;
                    }
                    n=s;
               }
               if(s==1)
                printf(“YES”);
                else
                    printf(“NO”);
               }
             
            return 0;

}

Add a Comment

Your email address will not be published. Required fields are marked *