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

Krishnamurthy Number

 //Write a program to accept a number and check it is Krishnamurthy number or not.

//If the sum of the factorial of all the digits of a number is equal to the original number.

//example- 145 is a Krishnamurthy number. (1+24+120)=145

import java.util.*;

class krishnamurthy

{//class krishnamurthy 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

        int r=0,f=1,sum=0,k=n;//variable r to extract the digits,variable f to store the factorial,variable k to store the no

        while(k>0)//145>0 t//14>0 t//1>0 t//0>0 false

        {

            r=k%10;//145%10=5//14%10=4//1%10=1

            f=1;

            for(int i=1;i<=r;i++)//1

            {

                f=f*i;//1x1=1

            }

            

            sum=sum+f;//0+120=120//120+24=144//144+1=145

            k=k/10;//145/10=14//14/10=1//1/10=0

        }

        if(sum==n)//145==145 true

        System.out.println(n+" is a krishnamurthy no");

        else

        System.out.println(n+" is not a krishnamurthy 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 ...