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

In this blog post, we learn how to write a C program to check whether a number is strong number or not?. We will write the C program to find a strong Number. How to check strong numbers using loop in C programming. Logic to check strong number in C programming.

Example,

Input: 145
Output: Yes it is a strong number
Explanation: 1! + 4! + 5! = 145


Input: 124
Output: No it is not a strong number
Explanation: 1! + 2! + 4! = 27 which is not equal to n i.e, 124

 

What is a strong number?

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

 

Logic to check strong number:

  1. Ask the user to enter any number and create a copy of the entered number in tmpNum.
  2. Declare and initialize another variable sum with 0, where the sum is an integer variable.
  3. Get the last digit of the given number by performing the modulo division (%) and store the value in last_digit variable, likey last_digit= number % 10.
  4. Find factorial of last_digit and store factorial in a variable says fact.
  5. Add factorial to sum i.e. sum = sum + fact.
  6. Remove last digit by dividing the number by 10 i.e. num = num / 10.
  7. Repeat steps 3-6 until the number becomes 0.
  8. Now, after the loop check condition for a strong number. If (sum == tmpNum,) then the given number is a strong number otherwise not.

 

C program to check whether a number is strong number or not:

The below program ask the user to enter the value. After getting the value from the user it will check whether the given number is a strong number or not by using the above-mentioned logic.

#include <stdio.h>

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

    //Get input a number from user
    printf("Enter any number: ");
    scanf("%d", &num);

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

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

        //Get last digit of num
        last_digit = num % 10;

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

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

        num = num / 10;
    }

    //Check Strong number condition
    if(sum == tmpNum)
    {
        printf("%d is strong number", tmpNum);
    }
    else
    {
        printf("%d is not strong number", tmpNum);
    }

    return 0;
}

Output:

Enter any number: 145
145 is strong number

 

C program to check a strong number by optimizing way:

Below,  I am writing an optimized way to find a strong number. I am also mentioning some steps for the same which help you to understand the code,

Steps to find the strong number:

  1. Compute the factorial of 0 -9 numbers and store in an array.
  2. Now ask the user to enter any number and create a copy of the entered number in tmpNum.
  3. Declare and initialize another variable sum with 0, where the sum is an integer variable.
  4. Get the last digit of the given number by performing the modulo division (%) and store the value in last_digit variable, likey last_digit= number % 10.
  5. Find factorial of last_digit using the pre-compute array.
  6. Add factorial to sum i.e. sum = sum + fact.
  7. Remove last digit by dividing the number by 10 i.e. num = num / 10.
  8. Repeat steps 4-7 until the number becomes 0.
  9. Now, after the loop check condition for a strong number. If (sum == tmpNum,) then the given number is a strong number otherwise not.

 

#include <stdio.h>

// Fills factorials of digits from 0 to 9.
void preComputeFact(int *fact)
{
    int i;
    fact[0] = fact[1] = 1;
    for (i = 2; i<10; ++i)
    {
        fact[i] = fact[i-1] * i;
    }
}

// Returns 1 if num is Strong
int isStrong(int num, const int *fact)
{
    int sum = 0;

    // Traverse through all digits of num.
    int tmpNum = num;
    while (tmpNum)
    {
        sum += fact[tmpNum%10];
        tmpNum /= 10;
    }

    return (sum == num);
}

int main()
{
    int num;
    int fact[10] = {0};

    //Compute fact 0-9
    preComputeFact(fact);

    //Get input a number from user
    printf("Enter any number: ");
    scanf("%d", &num);

    isStrong(num,fact) ? printf("Strong number\n") : printf("Not a Strong number\n");

    return 0;
}

Output:

Enter any number: 145
Strong number