Learn how to verify if a number is strong with C

In programming, a strong number is a special number whose sum of the factorial of every digit is equal to the original number. For example:

``!1 + !4 + !5 = 145``

And nope, the `!` is not the negation unary operator of programming, this means a factorial number (read how to know the factorial of a number in C here) in mathematics. In this article, we'll explain to you a pretty useful snippet in C that allows you to determine whether a number is strong or not.

## Implementation

The logic of the code to determine if a number is strong may be tricky for newbies, but once explained it's pretty easy to do. Our code to determine whether a number is strong is the following:

``````#include<stdio.h>

// Factorial function that returns the factorial of a given integer
int fact(int n){
int i,fac=1;

for(i=1;i<=n;++i){
fac*=i;
}

return fac;
}

int main(){
int number, lastDigit, sum, count;
sum = 0;

printf("Provide the number to determine:");
scanf("%d",&number);

count = number;

while(count != 0){
// Obtain the last digit of the number
lastDigit = count % 10;

// Keep the count of the sum of the factorial of the last digit
sum += fact(lastDigit);

// Divide the count to know when the loop should end
// e.g 145/10 = 14.5
// e.g 14.5/10 = 1.45
// e.g 1.45/10 = 0.145
// This means the loop of 145, will iterate 3 times (because 0.145/10 = 0.014499999999999999).
count = count / 10;
}

// If the sum is equal to the given number, it's strong !
if(sum == number){
printf("The number is Strong");
}else{
printf("The number is NOT strong");
}

return 0;
}
``````

The code works like this: our program will prompt our user for an integer that will be stored in the `number` variable. Now, for calculation purposes assign the value of the number to another variable that we named `count`. We defined as well a variable named sum that will contain the `sum` of the factorial of all the digits of the given number, this variable will have as initial value 0.

In order to find the last digit of a number mathematically, you can do this through the modulo operator and the remaining of the processed number will be the last digit, we'll store this value on the `lastDigit` variable. Now we will create a loop that will do 2 things until the count variable is equal to 0:

1. Find the factorial of the last digit and add it to the sum.
2. Remove the last digit from the number that we don't need (update `count` value)

Finally, once the loop ends, you will be able to compare whether the number is strong or not, comparing the `sum` variable with the given number at the beginning of our code, and that's it !

Happy coding !