Java - Happy Number Program.

 


 Happy Number Program.


import java.util.Scanner;
class  HappyNum
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Number");
        int num=sc.nextInt();
        int rem=0;
        int sqr=0;
        while(num>9)
        {
            int sum=0;
            while(num>0)
            {
                rem=num%10;
                sqr=rem*rem;
                sum=sum+sqr;
                num=num/10;
            }
            num=sum;
        }
        if(num==1)
        {
            System.out.println("Happy Number");
        }
        else
        {
            System.out.println("Not Happy");
        }
    }
}

 

Popular posts from this blog

In this problem, you will practice your knowledge on interfaces - Hacker Rank Solution.

Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

Query the Name of any student in STUDENTS who scored higher than 75 Marks - Hacker Rank Solution.