In the previous article, we have discussed Java Program to Check ISBN Number
In this article we are going to understand what Krishnamurthy number is and how we can check whether a number is Krishnamurthy or not in Java with examples.
Program to Check Krishnamurthy Number
Krishnamurthy numbers are numbers whose factorial of individual digits adds up to the number itself.
Example : 145: 1!+4!+5!= 1+24+120 = 145 Krishnamurthy number 19: 1!+9!=1+362880=362881 Not a Krishnamurthy number 124: 1!+2!+4!= 1+2+24=27 Not a Krishnamurthy number
In the above examples the numbers 19 and 124 are not Krishnamurthy numbers as their factorials don’t add up to the numbers. Hence 145 is the only Krishnamurthy number here.
Let’s see different ways to check Krishnamurthy number
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach :
- Enter/declare a number and store it.
- We calculate the factorials of each digit using a function and add them.
- If the resultant sum is the same as the entered number, then the number is said to be a Krishnamurthy number.
Method-1: Java Program to Check Krishnamurthy Number By Using Static Value
import java.util.Scanner; public class KrishnamurthyNumber{ public static void main(String args[]) { //A number declared int num = 145; int temp = num,remainder, sum =0; //Loop to iterate through digits and add their factorials while(temp>0) { remainder = temp%10; sum+= factorialOf(remainder); temp = temp /10; } if(sum==num) { System.out.println(num+" is a Krishnamurthy number"); } else { System.out.println(num+" is not a Krishnamurthy number"); } } // Function that returns the factorial of the number static int factorialOf(int num) { int prod = 1; while(num>0) { prod = prod*num; num--; } return prod; } }
Output: 145 is a Krishnamurthy number
Method-2: Java Program to Check Krishnamurthy Number By User Defined Method
import java.util.Scanner; public class KrishnamurthyNumber{ public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int temp = num,remainder, sum =0; //Loop to iterate through digits and add their factorials while(temp>0) { remainder = temp%10; sum+= factorialOf(remainder); temp = temp /10; } if(sum==num) { System.out.println(num+" is a Krishnamurthy number"); } else { System.out.println(num+" is not a Krishnamurthy number"); } } // Function that returns the factorial of the number static int factorialOf(int num) { int prod = 1; while(num>0) { prod = prod*num; num--; } return prod; } }
Output: Case-1 Enter a number : 145 145 is a Krishnamurthy number Case-2 Enter a number : 146 146 is a Krishnamurthy number
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: