Skip to content

Commit

Permalink
Merge pull request #1 from jspmic/krishna
Browse files Browse the repository at this point in the history
Added krishnamurthy.c in /math/
  • Loading branch information
jspmic authored Dec 1, 2023
2 parents e5dad3f + 67fe54d commit be91dd7
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions math/krishnamurthy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>

/*
* A krishnamurthy number is a number where the sum of
the factorial of its digits is equal to the number itself
For example, 145 = 1! + 4! + 5!
but 13 != 1! + 3!
* For more information, visit https://www.geeksforgeeks.org/problems/krishnamurthy-number1323/1
*/


int factorial(int n){
//Simple function returning the factorial of n

if (n<2){
return n;
}
return n*factorial(n-1);
}

int get_digit(int n, int sum){
// Function returning the sum of the factorial of the digits

if (n<10){
return sum+factorial(n);
}
else{
int div = n/10;
sum += factorial(n%10);
return get_digit(div,sum);
}
}

void krishnamurthy(int n){
int sum=0;
if (get_digit(n,sum)==n){
printf("%d is a krishnamurthy number\n",n);
}
else {
printf("%d is not a krishnamurthy number\n",n);
}
}

int main(){
int n = 13, x = 145;
printf("For example: \n");
krishnamurthy(n);
krishnamurthy(x);
return 0;
}

0 comments on commit be91dd7

Please sign in to comment.