C program to check whether a number is Strong number or not

Write a C program to enter any number and check whether number is Strong number or not. C program for strong numbers.

Required knowledge:

Basic C programming, If else, For loop

Strong number:

Strong numbers are the numbers whose sum of factorial of digits is equal to the original number.
Example: 145 is a strong number
Since 1! + 4! + 5! = 145

Program:

/**
 * C program to check whether a number is Strong Number or not
 */

#include <stdio.h>

int main()
{
    int i, n, num, sum=0;
    long fact;

    /*
     * Reads a number from user
     */
    printf("Enter any number to check Strong number: ");
    scanf("%d", &n);

    num = n;

    /*
     * Calculates sum of factorial of digits
     */
    while(n!=0)
    {

        /*
         * Finds the factorial
         */
        fact = 1;
        for(i=1; i<=n%10; i++)
        {
            fact = fact * i;
        }

        /* Adds the factorial to sum */
        sum = sum + fact;

        n = n/10;
    }

    /* 
     * If sum of factorial of digits is equal to original number then it is Strong number
     */
    if(sum==num)
    {
        printf("\n%d is Strong number", num);
    }
    else
    {
        printf("\n%d is not Strong number", num);
    }

    return 0;
} 
Output
X
_
Enter any number to check Strong number: 145

145 is Strong number

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment