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

Write a C program to input number from user and check whether number is Strong number or not. How to check strong numbers using loop in C programming. Logic to check strong number in C programming.

Example

Input

Input number: 145

Output

145 is STRONG NUMBER

Required knowledge

Basic C programming, If else, For loop, While loop, Nested loop

What is Strong number?

Strong number is a special number whose sum of factorial of digits is equal to the original number.
For example: 145 is strong number. Since, 1! + 4! + 5! = 145

Logic to check Strong number

Step by step descriptive logic to check strong number.

  1. Input a number from user to check for strong number. Store this in a variable say num. Copy it to a temporary variable for calculations purposes, say originalNum = num.
  2. Initialize another variable to store sum of factorial of digits, say sum = 0.
  3. Find last digit of the given number num. Store the result in a variable say lastDigit = num % 10.
  4. Find factorial of lastDigit. Store factorial in a variable say fact.
  5. Add factorial to sum i.e. sum = sum + fact.
  6. Remove last digit from num as it is not needed further.
  7. Repeat steps 3 to 6 till num > 0.
  8. After loop check condition for strong number. If sum == originalNum, then the given number is Strong number otherwise not.

Learn more – Program to find sum of digits of number.

Program to check Strong number

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

#include <stdio.h>

int main()
{
    int i, originalNum, num, lastDigit, sum;
    long fact;

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

    /* Copy the value of num to a temporary variable */
    originalNum = num;

    sum = 0;

    /* Find sum of factorial of digits */
    while(num > 0)
    {

        /* Get last digit of num */
        lastDigit = num % 10;

        /* Find factorial of last digit */
        fact = 1;
        for(i=1; i<=lastDigit; i++)
        {
            fact = fact * i;
        }

        /* Add factorial to sum */
        sum = sum + fact;

        num = num / 10;
    }

    /* Check Strong number condition */
    if(sum == originalNum)
    {
        printf("%d is STRONG NUMBER", originalNum);
    }
    else
    {
        printf("%d is NOT STRONG NUMBER", originalNum);
    }

    return 0;
}

Output

Enter any number to check Strong number: 145
145 is STRONG NUMBER

Happy coding 😉