Example:
Input lower limit: 1
Input upper limit: 100
Output strong numbers: 1, 2, 145
Required knowledge
Basic C programming, FunctionLogic to print strong numbers in range
We have already seen what are Strong numbers and how to print Strong numbers in given interval using loop also you must be knowing how to find factorial of any number. So, here what we need to do is write down entire logic of printing strong numbers in given interval into an user defined function.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; /* Reads starting and ending 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; } /** * Prints 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; //Finds sum of factorial of digits while(num != 0) { sum += fact(num % 10); num /= 10; } //Checks if sum of factorial of digits equal to current number if(start == sum) { printf("%d, ", start); } start++; } } /** * Recursively finds factorial of any number */ long long fact(int num) { if(num == 0) return 1; else return (num * fact(num-1)); }
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,
Enter the upper limit to find strong number: 100000
All strong numbers between 1 to 100000 are:
1, 2, 145, 40585,
Happy coding ;)
You may also like
- Function and recursion programming exercises index.
- C program to check prime, armstrong and perfect numbers using functions.
- C program to print all prime numbers between given interval using functions.
- C program to print all armstrong numbers between given interval using functions.
- C program to print all perfect numbers between given interval using functions.
- C program to find power of any number using recursion.
- C program to print all natural numbers from 1 to n using recursion.
- C program to find sum of all natural numbers from 1 to n using recursion.
- C program to find reverse of any number using recursion.
- C program to check whether a number is palindrome or not using recursion.