Showing posts with label Happy Number. Show all posts
Showing posts with label Happy Number. Show all posts

Happy Number

 //Wap to accept a number and check it is a Happy Number or not

//A happy number can be defined as a number which will yield 1 when it is replaced by the sum of the 

//square of its digits repeatedly.

import java.util.*;

class happy

{//class happy opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no//31

        int r=0,sum=0,k;//variable r to extract the digit,variable sum and k to store the sum

        sum=n;//31

        while(sum>9)//31>9 true//10>9 true//1>9 false

        {

            k=sum;//31//10

            sum=0;//0//0

            while(k>0)//10>0 t//1>0 t//0>0 f

            {

                r=k%10;//10%10=0//1%10=1

                sum=sum+r*r;//0+0x0=0//0+1x1=1

                k=k/10;//10/10=1//1/10=0

            }

        }

        if(sum==1)//1==1 true

        System.out.println("It is a happy no");

        else 

        System.out.println("It is not a happy no");

    }//main method closes

}//class closes


Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...