-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jspmic/krishna
Added krishnamurthy.c in /math/
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|