Write a C program to print Strong numbers between 1 to n. C program to print all strong numbers between a given range. Logic to print strong numbers in a given range in C program.

**Example**

**Input**

Input upper limit: 1000

**Output**

Strong numbers between 1-1000: 1, 2, 145

## Required knowledge

Basic C programming, If satement, For loop, Nested loop

Must know –

### What is a Strong number?

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

## Logic to print Strong numbers between 1 to n

Step by step descriptive logic to print strong numbers from 1 to n.

- Input upper limit to print strong number from user. Store it in a variable say
`end`. - Run a loop from 1 to
`end`, increment 1 in each iteration. Structure of the loop should be similar to`for(i=1; i<=end; i++)`

. - For each iteration inside loop check
`i`for strong number. Print the value of`i`if it is a strong number.

Let us convert the above logic to a C program.

## Program to print strong numbers between 1 to n

```
/**
* C program to print all Strong Numbers between 1 to n
*/
#include <stdio.h>
int main()
{
int i, j, cur, lastDigit, end;
long long fact, sum;
/* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &end);
printf("All Strong numbers between 1 to %d are:\n", end);
/* Iterate from 1 to end */
for(i=1; i<=end; i++)
{
/* Number to check for strong number */
cur = i;
sum = 0;
/* Find the sum of factorial of digits */
while(cur > 0)
{
fact = 1ll;
lastDigit = cur % 10;
/* Find factorial of last digit of current num. */
for( j=1; j<=lastDigit; j++)
{
fact = fact * j;
}
sum += fact;
cur /= 10;
}
/* Print 'i' if it is strong number */
if(sum == i)
{
printf("%d, ", i);
}
}
return 0;
}
```

Once, you got the logic of printing strong numbers between 1 to `n`. You can easily modify the logic to find strong numbers in any given range. Below program illustrates how to print strong numbers in a given range.

## Program to print strong numbers in given range

```
/**
* C program to print Strong numbers in given range
*/
#include <stdio.h>
int main()
{
int i, j, cur, lastDigit, start, end;
long long fact, sum;
/* Input lower and upper limit from user */
printf("Enter lower limit: ");
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);
printf("All Strong numbers between %d to %d are:\n", start, end);
/* Iterate from 1 to end */
for(i=start; i<=end; i++)
{
/* Number to check for strong number */
cur = i;
sum = 0;
/* Find the sum of factorial of digits */
while(cur > 0)
{
fact = 1ll;
lastDigit = cur % 10;
/* Find factorial of last digit of current num. */
for( j=1; j<=lastDigit; j++)
{
fact = fact * j;
}
sum += fact;
cur /= 10;
}
/* Print 'i' if it is strong number */
if(sum == i)
{
printf("%d, ", i);
}
}
return 0;
}
```

Output

Enter lower limit: 1 Enter upper limit: 100000 1, 2, 145, 40585,

Happy coding 😉

<pre><code> ----Your Source Code---- </code></pre>