# C Program to Find Strong Number

We will write a C program to find a Strong number or Krishnamurthy Number. After that, we will write another C program to find a Strong Number in a given Range. First of all, we should know that what is Strong number? Prerequisite examples are factorial of a number and sum of digits in a given number.

Strong Number:- The sum of the factorial of individual digits of a number is equal to the same number. Sometimes the Strong number also called Krishnamurthy Number.

Example:-
145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
So, 145 is a strong number.

234 = 2! + 3! + 4! = 2 + 6 + 24 = 32
So, 234 is not a strong number.

40585 = 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585
So, 40585 is a strong number.

## C program to check the number is Strong number or not

``````#include<stdio.h>
int main()
{
int number, tempvariable, remainder, i, factorial, sum=0;

printf("Enter number: ");
scanf("%d",&number);

tempvariable = number;

while( number!=0 )
{
factorial = 1;  //every time factorial initialize with 1
remainder = number%10;

for(i=1; i<=remainder; i++)
{
factorial *= i; //factorial=factorial * i
}

sum += factorial;  //sum= sum+factorial
number /= 10;  //numbar=numbar/10
}

if( tempvariable == sum )
printf("%d is a strong number.\n",tempvariable);
else
printf("%d is not a strong number.\n",tempvariable);

return 0;
}``````

Output:-

Enter number: 145
145 is a strong number.

Enter number: 500
500 is not a strong number.

Explanation:- The variables for this program are number, tempvariable, remainder, i, factorial, and sum. Variable tempvariable holds the value of the number, because finally we need to check that result is the same as the previously given number or not? Using while loop iteration starts with the condition that the variable number should not be equal to 0. We need to find the factorial of each digit in a given number.

After finding the factorial of each digit, the sum will be calculated. To find the factorial of each digit first we need to find the last digit of the variable number using the variable remainder. Now, factorial will be calculated. The addition of factorial of each digit will be calculated and stored into a variable sum.

Iteration will be ended when the number becomes equal to 0. Using an if-else conditional statement we will check that is the variable sum and the variable tempvariable is equal or not? If the answer is yes then the given number is a strong number otherwise it is not a strong number.

## Strong number in a given range

Now, We will find Strong number in a given range. The Range starts from m and end at n.

``````#include<stdio.h>
int main()
{
int m, n, i, num, j, fact, sum;

printf("Enter the range m and n Values (m<n): ");
scanf("%d %d",&m,&n);

printf("Strong numbers are:\n");

for(i=m; i<=n; i++)
{
num = i;
sum = 0;

while(num!=0)
{
fact=1;

for(j=1;j<=(num%10);j++)
{
fact *= j;
}

sum += fact; //sum=sum+fact
num /= 10; //num=num/10
}

if(sum==i)
printf("%d\n",i);
}

return 0;
}``````

Output:-

Enter the range m and n Values (m<n): 1, 1000000
Strong numbers are:
1
2
145
40585

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!