C Program to Check whether a Number is Strong or Not

It is a number in which the sum of the factorials of digits of the number is equal to the original number. 
E.g.: n=145=> 1! + 4! + 5! = 1 + 24 + 120 = 145 
so it is strong number.

#include<stdio.h>
#include<conio.h>

int factorial(int);

void main()
{
    int num,temp,rem,strong=0;
    printf("Enter a number:");
    scanf("%d",&num);
    temp=num;
    while(num>0)
    {
        rem=num%10;
        strong=strong+factorial(rem);
        num=num/10;
    }
    if(temp==strong)
        printf("%d is strong number",temp);
    else
        printf("%d is not strong number",temp);
    getch();
}

int factorial(int r)
{
    int i,f=1;
    for(i=r;i>=1;i--)
    {
        f=f*i;
    }
    return f;
}

OUTPUT:
Enter a number:23
23 is not strong number

Enter a number:145

No comments:

Post a Comment