C program To Check whether a Number is Strong Number or Not

Lets write a C program to check whether user entered number is strong number or not, using nested while loop.

Strong Number: Sum of factorial of a number’s individual digits should be equal to the number itself. Such a number is called Strong Number.

For Example: If user entered number is 145. We find factorial of individual digits of 145 and add it.
i.e., !1 + !4 + !5

(1) + (24) + (120) = 145

So the original number and the sum of factorials of individual digits are both 145. So number 145 is considered as a Strong Number.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
Nested While Loop: C Program

Important Topics
Calculate Sum of Digits: C Program
C Program To Find Factorial of a Number

Source Code: C program To Check whether a Number is Strong Number or Not

#include < stdio.h >

int main()
{
    int num, temp, rem, count, fact, sum = 0;

    printf("Enter a number\n");
    scanf("%d", &num);

    temp = num;

    while(num)
    {
        rem = num % 10;

        count = 1;
        fact  = 1;
        while(count <= rem)
        {
            fact = fact * count;
            count++;
        }

        printf("Factorial of %d is %d\n", rem, fact);

        sum = sum + fact;

        num = num / 10;
    }

    if(temp == sum)
    {
        printf("%d is a strong number\n", temp);
    }
    else
    {
        printf("%d is not a strong number\n", temp);
    }

    return 0;
}

Output 1
Enter a number
145
Factorial of 5 is 120
Factorial of 4 is 24
Factorial of 1 is 1
145 is a strong number

Output 2
Enter a number
140
Factorial of 0 is 1
Factorial of 4 is 24
Factorial of 1 is 1
140 is not a strong number

Video Tutorial: C Program To Find Strong Number


[youtube https://www.youtube.com/watch?v=u3yCwqjFCPQ]

YouTube Link: https://www.youtube.com/watch?v=u3yCwqjFCPQ [Watch the Video In Full Screen.]

Logic To Check whether a Number is Strong Number or Not

If user entered number is 145. i.e., num = 145. Using outer while loop we keep fetching digits one by one by using below code.

rem = num % 10;
num = num / 10;

For each iteration the outer loop fetches individual digits of the number. The inner while loop calculates the factorial for that fetched individual digit. Next we keep adding the factorial of each individual digit.

Once the control exits outer while loop we check if the user entered number is equal to the value present in variable sum. If true, then the user entered number is a strong number, if not, its not a strong number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert