Krishnamurthy Number Java

Krishnamurthy number is another special number in Java. A number is said to be Krishnamurthy if the factorial sum of all its digits is equal to that number. Krishnamurthy number is also referred to as a Strong number. Just like Prime and Armstrong numbers, Krishnamurthy number is also frequently asked by the interviewers but with it’s another name, i.e., Strong number.

These are the following steps which we use to check Krishnamurthy number

  • We first take a number.
  • We then find the factorial of each digit of the number.
  • We add the factorial of each digit and store it into another variable.
  • If the sum of the factorial of each digit is the same as the original number, the number is a Krishnamurthy number.

Let’s take an example of Krishnamurthy number.

  1. Number = 145  
  2. = 1! + 4! + 5!  
  3. = 1 + ( 4 * 3 * 2 * 1 ) + ( 5 * 4 * 3 * 2 * 1 )  
  4. = 1 + 24 + 120  
  5. = 145  

Let’s implement a program to check whether the user entered number is a Krishnamurthy number or not.

KrishnamurthyNumber.java

  1. //import required classes and packages  
  2. import Java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create KrishnamurthyNumber class to check whether the given number is a Krishnamurthy number or not  
  7. class KrishnamurthyNumber {   
  8.       
  9.     // create fact() method to calculate the factorial of the number   
  10.     static int fact(int number)   
  11.     {   
  12.         int f = 1;   
  13.         while (number != 0) {   
  14.             f = f * number;   
  15.             number–;   
  16.         }   
  17.         return f;   
  18.     }   
  19.   
  20.     // create checkNumber() method that returns true when it founds number krishnamurthy   
  21.     static boolean checkNumber(int number)   
  22.     {   
  23.         int sum = 0;    //initialize sum to 0   
  24.           
  25.         int tempNumber = number;    //create a copy of the original number   
  26.           
  27.         //perform operation until tempNumber will not equal to 0  
  28.         while (tempNumber != 0) {   
  29.             // calculate the factorial of the last digit of the tempNumber and then add it to the sum  
  30.             sum = sum + fact(tempNumber % 10);   
  31.   
  32.             // replace the value of tempNumber by tempNumber/10   
  33.             tempNumber = tempNumber / 10;   
  34.         }   
  35.   
  36.         // Check whether the number is equal to the sum or not. If both are equal, number is krishnamurthy number  
  37.         if(sum == number)  
  38.             return true;  
  39.         else  
  40.             return false;  
  41.     }   
  42.   
  43.     // main() method start  
  44.     public static void main(String[] args)   
  45.     {   
  46.         int n;  //initialize variable n  
  47.           
  48.         //create scanner class object to read data from user  
  49.         Scanner sc = new Scanner(System.in);  
  50.           
  51.         //custom message  
  52.         System.out.println(“Enter any number:”);  
  53.           
  54.         //store user entered value into variable n  
  55.         n = sc.nextInt();  
  56.          
  57.         if (checkNumber(n))   
  58.             System.out.println(n + ” is a krishnamurthy number”);   
  59.         else  
  60.             System.out.println(n + “is not a krishnamurthy number”);   
  61.     }   
  62. }  

Output

Krishnamurthy Number Java

Let’s implement one more program to get all the Krishnamurthy number in a given range

FindAllKrishnamurthyNumber.java

  1. //import required classes and packages  
  2. import java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create FindAllKrishnamurthyNumber class to get all the Krishnamurthy number in a given range  
  7. class FindAllKrishnamurthyNumber  
  8. {  
  9.     //main() method start  
  10.     public static void main(String args[])  
  11.     {  
  12.         int range;  
  13.           
  14.         //create scanner class object  
  15.         Scanner sc=new Scanner(System.in);  
  16.           
  17.         //show custom message  
  18.         System.out.println(“Enter the value of range”);  
  19.           
  20.         //store user entered value into variable range  
  21.         range = sc.nextInt();  
  22.   
  23.         for(int i = 1; i <= range; i++)  
  24.             checkNumber(i);  
  25.     }  
  26.   
  27.     // create fact() method to calculate the factorial of the number   
  28.     static int fact(int number)   
  29.     {   
  30.         int f = 1;   
  31.         while (number != 0) {   
  32.             f = f * number;   
  33.             number–;   
  34.         }   
  35.         return f;   
  36.     }   
  37.   
  38.     // create checkNumber() method to check krishnamurthy number  
  39.     static void checkNumber(int number)   
  40.     {   
  41.         int sum = 0;    //initialize sum to 0   
  42.           
  43.         int tempNumber = number;    //create a copy of the original number   
  44.           
  45.         //perform operation until tempNumber will not equal to 0  
  46.         while (tempNumber != 0) {   
  47.             // calculate the factorial of the last digit of the tempNumber and then add it to the sum  
  48.             sum = sum + fact(tempNumber % 10);   
  49.   
  50.             // replace the value of tempNumber by tempNumber/10   
  51.             tempNumber = tempNumber / 10;   
  52.         }   
  53.   
  54.         // Check whether the number is equal to the sum or not. If both are equal, the number is Krishnamurthy number  
  55.         if(sum == number)  
  56.             System.out.println(number + ” is a krishnamurthy number”);   
  57.     }  
  58. }  

Output

Krishnamurthy Number Java

Leave a Comment