C program to print all strong numbers between given interval using function

Write a C program to print all strong numbers between a given interval in C programming. How to print strong numbers in a given range using functions in C programming. Write a C function to find strong numbers between 1 to n.

Example

Input

```Input lower limit: 1
Input upper limit: 1000```

Output

`Strong numbers between 1 to 100: 1, 2, 145`

Required knowledge

Must know - Program to check strong number.

Declare function to find all strong numbers in given range

1. First give a meaningful name to our function, say `printStrongNumbers()`. Along with this we must define function to find factorial of a number, say `long long fact(int num);`.
2. Next we need to print strong numbers in range. Therefore, the function must accept two integer parameters start and end limit of strong numbers.
3. Finally the function simply prints strong numbers in range and returns nothing.

After looking on above points function declaration to print strong numbers in range is - void printStrongNumbers(int start, int end);.

Program to print strong numbers in given interval

``````/**
* C program to print strong numbers in a given interval using functions
*/

#include <stdio.h>

/* Function declaration */
long long fact(int num);
void printStrongNumbers(int start, int end);

int main()
{
int start, end;

/* Input start and end range */
printf("Enter the lower limit to find strong number: ");
scanf("%d", &start);
printf("Enter the upper limit to find strong number: ");
scanf("%d", &end);

printf("All strong numbers between %d to %d are: \n", start, end);
printStrongNumbers(start, end);

return 0;
}

/**
* Print all strong numbers in a given range
*/
void printStrongNumbers(int start, int end)
{
long long sum;
int num;

// Iterates from start to end
while(start != end)
{
sum = 0;
num = start;

// Calculate sum of factorial of digits
while(num != 0)
{
sum += fact(num % 10);
num /= 10;
}

// If sum of factorial of digits equal to current number
if(start == sum)
{
printf("%d, ", start);
}

start++;
}
}

/**
* Recursively find factorial of any number
*/
long long fact(int num)
{
if(num == 0)
return 1;
else
return (num * fact(num-1));
}``````

In the above program I have used recursive approach to find factorial. However, you can also use simple iterative approach to find factorial of a number.

Output

```Enter the lower limit to find strong number: 1
Enter the upper limit to find strong number: 100000
All strong numbers between 1 to 100000 are:
1, 2, 145, 40585,```

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside