Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number in java.

Krishnamurthy Number

A number is said to be a Krishnamurthy Number if the sum of the factorial of all digits of a number is equal to the original number itself. Krishnamurthy Number is also known as Strong number and is frequently asked questions in interviews.

Example:

Input: 145
Output: Krishnamurthy Number
= 1! + 4! + 5!
= 1 + 24 + 120
= 145
= Original Number


Java Program to check Krishnamurthy Number

import java.util.Scanner;

public class KrishnamurthyProgram
{
   // boolean function to check Krishnamurthy Number
   public static boolean isKrishnamurthy(int number)
   {
      int sum = 0, lastDigit = 0;
      int temp = number;

      // iterating through all the digits
      while (temp != 0)
      {
         lastDigit = temp % 10;
         sum += factFunc(lastDigit); //calling factFunc func for every digit
         temp /= 10;
      }

      // returns true if number and sum are equal
      if (sum == number)
         return true;
      return false;
   }

   //user defined function to calculate the factorial
   public static long factFunc(int n)
   {
      long fact = 1;
      for (int i = 1; i <= n; i++)
         fact *= i;

      return fact;
   }

   // main function
   public static void main(String[] args)
   {
      int num = 0;
      boolean checkResult = false;


      Scanner scan = new Scanner(System.in);

      // user input
      System.out.print("Enter an integer: ");
      num = scan.nextInt();

      // calling function by passing the entered number
      checkResult = isKrishnamurthy(num);

      //Check and display the result
      if (checkResult)
         System.out.println(num + " is a Krishnamurthy number");
      else
         System.out.println(num + " is not a Krishnamurthy number");

   }
}

Output:

//Run: 1
Enter an integer: 145
145 is a Krishnamurthy number

//Run: 2
Enter an integer: 123
123 is not a Krishnamurthy number

We have created two different user-defined functions, one to calculate the factorial of a number and another to check the number for Krishnamurthy which is a boolean function.


Java Program to check Krishnamurthy number within the given Range

import java.util.Scanner;

public class KrishnamurthyProgram
{
   // main function
   public static void main(String[] args)
   {
      int lrRange, upRange;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter lower Range value: ");
      lrRange = scan.nextInt();
      System.out.print("Enter Upper Range value: ");
      upRange = scan.nextInt();

      System.out.println("The Krishnamurthy number between " + lrRange + " and " + upRange + " are: ");

      for (int i = lrRange; i <= upRange; i++)
      {
         if (isKrishnamurthy(i))
            System.out.print(i + " ");
      }
   }

   // boolean function to check Krishnamurthy Number
   public static boolean isKrishnamurthy(int num)
   {
      int sum = 0, lastDigit = 0;
      int temp = num;

      // iterating through all the digits
      while (temp != 0)
      {
         lastDigit = temp % 10;
         sum += factFunc(lastDigit);	//calling factFunc func for every digit
         temp /= 10;
      }

      // compare sum and number
      if (sum == num)
         return true;
      return false;
   }

   //user defined function to calculate the factorial
   public static long factFunc(int n)
   {
      long fact = 1;
      for (int i = 1; i <= n; i++)
         fact *= i;

      return fact;
   }
}

Output:

Enter lower Range value: 1
Enter Upper Range value: 1000
The Krishnamurthy number between 1 and 1000 are: 
1 2 145

As you can see, we did the same way by creating separate functions but here we took the user input for both the lower and upper range and called the function within those ranges to find all the possible Krishnamurthy number java.