0

I just want to know that for finding the Krishnamurthy number, we have to first find the factorial of the digits, then the addition of those numbers. (like, 1!+4!+5! = 145).

So, below is my code, and I have applied a factorial function over there. But the output is not coming in favor (145 is not a Kri...).

    #include <stdio.h>
#include <stdlib.h>

void main()
{
    int digit,factorial = 1, temp, input, sum = 0;
    printf("Enter a Number:\n");
    scanf("%d",&input);
    int Factorial(int digit){

         factorial = factorial*digit;
         return 0;
    }
    temp = input;
    while(temp>0){
        digit = temp%10;
        temp = temp/10;
        sum = sum + Factorial(digit);
    }
    if(sum==input){
        printf("%d is a Krishnamurthy Number",input);
    }
    else{
        printf("%d is not a Krishnamurthy Number",input);
    }


}

Have I done anything wrong in logic, or function declaration or definition? Please help.

  • 1
    You can not have funtion inside a function. – Eraklon Feb 27 '20 at 16:49
  • @Eraklon yes, i have changed that but still not getting the desired output – ChattTheDev Feb 27 '20 at 16:53
  • 1
    Not sure what you are doing with the `Factorial` function (e.g. 4! = 4*3*2*1 so how does that function produce 24?) - but since there are only 10 digit possibilities why not create an array[10] of the 10 possible factorials and index it by `digit`. –  Feb 27 '20 at 16:53
  • Is the set of Krishnamurthy numbers finite? –  Feb 27 '20 at 17:07

2 Answers2

0

your factorial function is not performing correctly. factorial means, multiplication of all digits starting from n downto 1 -

(n-1) * (n-2) * ... * (n)

but your function is not giving the desired result you want.

int Factorial(int digit){
     factorial = factorial*digit;
     return 0;
}

you need to change that function to get the factorial value of a number, you can either iterate a loop downto one or use a recursive approach to get the factorial.

int Factorial(int digit){
     int result = 1;
     for(int i=n; i>=1; i--){
        result *= i;
     }
     return result;
}

or

int Factorial(int digit) {
    if(n <= 1) return digit;

    return digit * Factorial(digit-1);
}

you can follow the thread to understand the depth of recursive function mentioned above.

Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17
0
#include<stdio.h>
int main(int argc, char* argv[], char* envp[])
{
int sum = 0, 
int a, 
int p = 0, 
int d, 
int i, 
int fact;

//code
printf("Enter a number: ");
scanf("%d", &a);
p = a;

while (a > 0)
{
fact = 1;
    d = a % 10;
    a /= 10;
    for (i = d; i >= 1; i--)
    {
        fact *= i;
    }
    sum += fact;
}

if (sum == p)
    printf("It is a Krishnamurthy number.\n");
else
    printf("It is not a Krishnamurthy number.\n");

printf("\n\n");

return(0);
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 10 '21 at 15:18