C Program To Print Strong Numbers From 1 To N
Let us learn how to print strong numbers from 1 to n in C programming language. This code below is to check if a number is a strong integer or not in C programming using functions, while and for loops.
It is important that we should know how a for loop works before getting further with the C program. A strong number makes use of the factorial in C programming.
What is a Strong Number?
A number is said to be a strong number if the sum of the factorials of the digits of a number is equal to the number itself.
Example
145 = (1!) + (4!) + (5!)
40585 = (4!) + (0!) + (5!) + (8!) + (5!)
Must Read: C Program To Check if a Number is Strong Number
C Program To Print Strong Numbers from 1 To N
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> int main() { int count, j, temp, limit, sum = 0, fact; printf("\nEnter Limit:\t"); scanf("%d", &limit); printf("\nStrong Numbers Between 1 To %d\n", limit); for(count = 1; count <= limit; count++) { temp = count; while(temp != 0) { fact = 1; for(j = 1; j <= temp%10; j++) { fact = fact * j; } sum = sum + fact; temp = temp/10; } if(sum == count) { printf("%d\t", count); } } printf("\n"); return 0; } |
Must Read: C Program To Calculate GCD of Two Integers
Output
In case you get any compilation errors or any doubts in this C program to find strong integers, let us know about it in the comment section below.
Such a simple and easy program for finding strong numbers between 1 to 100. Can we use a separate function for calculating factorial to make it more modular?
You can definitely make a separate function to calculate factorial in this c program to print strong numbers from 1 to N. This technique will make your code more readable and easy to debug.