Showing posts with label happy-number. Show all posts
Showing posts with label happy-number. Show all posts

Sunday, August 23, 2020

Write a program to input a number and check whether it is Happy Number or not? (28=2^2+8^2=4+64=68,6^2+8^2=36+64=100,1^2+0^2+0^2=1+0+0=1)

83)  Write a program to input a number and check whether it is Happy Number or not? (28=22+82=4+64=68,62+82=36+64=100,12+02+02=1+0+0=1) 


import java.util.*;
public class Happy_num
{
    public static void main(String args[ ])
   {
           Scanner sc = new Scanner (System.in);
           System.out.println("Enter a number");
           int a = sc.nextInt();
           int s = 10;
           while(s>9)
           {
               s=0;
               while(a>0)
               {
                   int d = a%10;
                   int x = (int)Math.pow(d,2);
                   a = a/10;
                   s=s+x;
               }
               a=s;
           }
           if(s==1)
           {
               System.out.println("Entered number is a HAPPY Number");
           }
           else
           {
               System.out.println("Entered number is not a HAPPY Number");
           }
    }
}


Output : 





 jhvghvgk ;nkjn ,nkjhj