In this tutorial, we will learn about the strong number and write a C Program to find Strong Number.
Strong Number
Strong numbers are those numbers whose sum of the factorial of the individual digits is equal to the number itself.
Example: 145 is a strong number.
Explanation: 145
1! + 4! + 5!
= 1 + 24 + 120
= 145, number itself.
Hence it is a Strong number.
Let us understand the Strong number in C program.
C program to check whether a number is Strong number or not
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 30 31 32 33 34 35 36 37 38 | #include<stdio.h> int main() { int num, rem, temp; int i, sum = 0, fact; printf("Enter the number to be checked: "); scanf("%d", &num); temp = num; while(temp) { i = 1, fact = 1; rem = temp%10; //calculation of factorial while(i <= rem) { fact = fact * i; i++; } sum += fact; temp /= 10; } // displaying according to condition if(sum == num) printf("%d is a strong number.", num); else printf("%d is not a strong number.", num); return 0; } |
Output:
Enter the number to be checked: 145
145 is a strong number.
Let us go through another example where we display all the strong numbers within an entered range.
C Program to print strong numbers in a given range
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | // program to find strong numbers within a given range in C #include <stdio.h> int main() { int lrRange, upRange, rem, temp; int i, sum = 0, fact; printf("Enter the lower range: "); scanf("%d", &lrRange); printf("Enter the upper range: "); scanf("%d", &upRange); printf("Strong number between entered range are: "); for (i = lrRange; i <= upRange; i++) { temp = i; while (temp) { fact = 1; rem = temp % 10; //calculation of factorial int count = 1; while (count <= rem) { fact = fact * count; count++; } sum += fact; temp /= 10; } //Display if (sum == i) printf("%d ", i); sum = 0; } return 0; } |
Output:
Enter the lower range: 1
Enter the upper range: 300
Strong number between entered range are: 1 2 145