Programming Examples

Java program to check number is Krishnamurti number or not


Write a program to print all the Krishnamurti number from 1 to n. Here, n is user dependent. A Krishnamurti number is a number whose sum of factorial of individual digits equals the number.

For example, 145 = 1! + 4! + 5! = 1 + 24+ 120 = 145.

Solution

import java.util.*;
class KrishnamurtiNumber
{
    int factorial(int n)
    {
        int fact=1;
        while(n>=1)
        {
            fact=fact*n;
            n--;
        }
        return fact;
    }
    public static void main(String arr[])
    {
        KrishnamurtiNumber obj=new KrishnamurtiNumber();
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any Number :");
        int num=sc.nextInt();
        int temp=num;
        int digit,sum=0;
        while(num>0)
        {
            digit=num%10;
            sum=sum+obj.factorial(digit);
            num=num/10;
        }
        if(temp==sum)
        {
            System.out.println(" NUmber is a Krishnamurti Number ") ;   
        }
        else
        {
             System.out.println(" NUmber is a Krishnamurti Number ") ;    
        }
    }
}
Output

Enter any Number : 145

Number is a Krishnamurti Number

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz