# Strong Number in C

## What is Strong Number

Before writing a program to find a strong number in c programming, we must know the exact definition of a strong number. A strong number is a number whose sum of the factorial of its digits is equal to that number. Confuse? Let’s take an example. Suppose, we have a number num=145. Now, as per the definition of a strong number, find the addition of factorial of digits of 145. The addition is 1!+4!+5!=1+24+120=145. So, the answer is 145 and our original number was 145. Both are equal so we can say 145 is a strong number.

These strong numbers are very rare. The strong numbers between 1 to 1000 are 1, 2 and 145 only. Let’s take another example.

### Is 40585 a strong number?

To know this, we must find the addition of factorials of all the digits of 40585 and if the addition comes 40585, then we can say 40585 is a strong number.

The expression is 4!+0!+5!+8!+5! = 24+1+120+40320+120 = 40585. Therefore, we can say, 40585 is a strong number. Most people calculate this answer 40584 and the reason is they consider factorial of 0 is 0. But it is wrong. Always remember that factorial of 0 is 1.

Also Read: Palindrome in C using Pointers

For writing a program to find strong number in c, we will see some important steps. These steps will help you understand the program.

### Algorithm for strong number in c

1. Read a number from the user.
2. Copy that number to another variable.
3. Use while loop to separate the digits.
4. After separating digits, find its factorial and add to the variable sum.
5. Repeat step 4 until the while loop terminates.
6. Now, compare the original number and the sum in the if statement.
7. If both the numbers are equal then display message that the number is “strong number”, otherwise “not a strong number”

If you understood these steps, then you can easily write a program to check strong number in c programming.

Also Read: Floyd Triangle in C Programming

### Program to Find Strong Number in C using while loop

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum=0,fact,n,num,rem;
printf("Enter a Number\n");
scanf("%d",&n);
num=n;
while(n>0)
{
rem=n%10;
fact=1;
for(i=1;i<=rem;i++)
{
fact=fact*i;
}
sum=sum+fact;
n=n/10;
}
if(num==sum)
{
printf("%d is a Strong Number",num);
}
else
{
printf("%d is not a Strong Number",num);
}
return 0;
}
``````

#### Output

``````Enter a Number
145
145 is a Strong Number
Enter a Number
343
343 is not a Strong Number``````

### Detailed Explanation of the Program

Variable Declaration

``````int i, sum=0,fact,n,num,rem;
``````

Here, the variable i is the loop variable. The sum is a variable in which addition of factorials of digits are stored. The variable fact which contains factorial of a digits. The variable n contains the number which is entered by the user and that value is assigned to other variable num. When we need to separate digits, we will use modulus operator to get the remainder.

Also Read: Swapping of Two Numbers in C

``````printf("Enter a Number\n");
scanf("%d",&n);
``````

The printf() displays message to the user and ask him to enter a number. Suppose, that user enter 145, then that number is stored in the variable n. Now, the value of n = 145.

``````num=n;
``````

Here, we are assigning the value of n to the variable num. Why? Because the value of n will be changed in the while loop. At the end we need the original number because we will have to compare the original number with the sum to decide whether the number is strong or not.

#### The while loop

``while(n>0)``

This loop will do three things:

1. Separate digits.
2. Calculate factorial of digits.
3. Find the addition of the factorial of the digits.
``````rem=n%10;
``````

This statement will separate digits. When you divide any number by 10, it gives the last digit. Here, the value of n is 145. After executing this statement, the value of rem will be 5. Now, we need to find the factorial of this 5.

Also Read: Factorial Program in C Programming

#### The for loop

``````fact=1;
for(i=1;i<=rem;i++)
{
fact=fact*i;
}``````

This code is used to calculate factorial. After executing this loop, factorial of 5 is 120. Therefore, the value of fact = 120. Now, add this value the variable sum.

``````sum=sum+fact;
``````

After executing this line, the value of sum = 120.

But, we need to find strong number in c, not factorial or sum of numbers. So, we need to repeat above steps. That is why, we are using while loop here.

``n=n/10;``

The while loop execution depends on the value of the variable n. We have separated and find factorial of 5 from the number 145. Now, we need another digit to be separated and that number is 4. Therefore, we need the above statement. In the above statement, the value of n becomes 14. When we divide any number by 10 then we get the quotient. Here, we are dividing the 145 by 10 and storing that quotient i.e. 14 in the variable n.

Again we reach to the while loop. The value of n is 14 which is greater than 0. Therefore, this loop will continue once again.

After completing the while loop, the value of n becomes 0 and the sum becomes 145.

#### Strong Number or Not

Now, it is time to check whether the number is strong or not. In the above while loop, we have calculated only the addition of factorials of digits of a number.

``````if(num==sum)
{
printf("%d is a Strong Number",num);
}
else
{
printf("%d is not a Strong Number",num);
}
``````

In the above code, we are comparing the value of num and sum. For example, the value of num is 145 and that of sum is also 145. So, here, the body of if statement will be executed. That means, we will get the message that 145 is a Strong Number.

In case of else statement, we will get the message that suppose 343 is not a Strong Number.

This was the explanation for finding the strong number in c programming using while loop. The same program can be written using the for loop.

### Strong number in c using for loop

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum=0,fact,n,num,rem;
printf("Enter a Number\n");
scanf("%d",&n);
for(num=n;num>0;num/=10)
{
rem=num%10;
fact=1;
for(i=1;i<=rem;i++)
{
fact=fact*i;
}
sum=sum+fact;
}
if(n==sum)
{
printf("%d is a Strong Number",n);
}
else
{
printf("%d is not a Strong Number",n);
}
return 0;
}
``````

In this c program, we are finding the strong number using the for loop. Now, we will see the same program using function.

### Strong number in c using function

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int strong(int);
int n,sum;
printf("Enter a Number\n");
scanf("%d",&n);
sum=strong(n);
if(n==sum)
{
printf("%d is a Strong Number",n);
}
else
{
printf("%d is not a Strong Number",n);
}
return 0;
}
int strong(int n)
{
int i,sum=0,fact,num,rem;
for(num=n;num>0;num/=10)
{
rem=num%10;
fact=1;
for(i=1;i<=rem;i++)
{
fact=fact*i;
}
sum=sum+fact;
}
return sum;
}
``````

In this c program, we are using a user-defined function to find a strong number. Here, we are reading a number from the user and sending that number to the called function where we are finding the addition of factorial of each and every numbers. This addition is stored in the variable sum and after this, we are returning this value to the calling function.

In the main function, we are comparing the value of n and sum. If both the values are equal then that number is a strong number.

So, this is a very interesting program which is finding a strong number in c programming. I hope, you have enjoyed this article or you can say tutorial. So, thank you very much for spending your time here.