Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created April 14, 2022 13:21
Show Gist options
  • Save aadipoddar/3d683bd5c6a32d8a599867140944988b to your computer and use it in GitHub Desktop.
Save aadipoddar/3d683bd5c6a32d8a599867140944988b to your computer and use it in GitHub Desktop.
Check is Number is Krishnamorty or not by using Recursion
/*
Check is Number is Krishnamorty or not by using Recursion
A krishnamurty Number is a number which
is the sum of the factorial of each digits of a number
is equal to the original number.
INPUT: 145
OUTPUT: Yes
*/
import java.util.*;
class KrishnamurtyRecursive {
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int krishnamurty(int n) {
if (n != 0)
return krishnamurty(n / 10) + factorial(n % 10);
else
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
if(n == new KrishnamurtyRecursive().krishnamurty(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment