From 67fe54d70d232f8ff3b91974c19bd977f5948a58 Mon Sep 17 00:00:00 2001 From: jspmic Date: Fri, 1 Dec 2023 12:36:05 +0200 Subject: [PATCH] Added krishnamurthy.c in /math/ --- math/krishnamurthy.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 math/krishnamurthy.c diff --git a/math/krishnamurthy.c b/math/krishnamurthy.c new file mode 100644 index 0000000000..cc65ef9122 --- /dev/null +++ b/math/krishnamurthy.c @@ -0,0 +1,55 @@ +#include + +/* + + * 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; +} +