Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program to find the given number is strong or not
A strong number is a number where the sum of the factorial of its digits equals the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
Syntax
int factorial(int n); int isStrongNumber(int num);
Understanding Strong Numbers
Let's examine some examples to understand the concept better ?
145 = 1! + 4! + 5!
= 1 + 24 + 120
= 145 (Strong number)
123 = 1! + 2! + 3!
= 1 + 2 + 6
= 9 (Not a strong number)
Example: Check if Number is Strong
This program calculates the sum of factorials of each digit and compares it with the original number ?
#include <stdio.h>
int main() {
int n, temp, rem, sum = 0, i, fact;
printf("Enter a number: ");
n = 145; /* Testing with 145 */
temp = n;
while (n) {
i = 1;
fact = 1;
rem = n % 10;
/* Calculate factorial of digit */
while (i <= rem) {
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}
if (sum == temp)
printf("%d is a strong number<br>", temp);
else
printf("%d is not a strong number<br>", temp);
return 0;
}
145 is a strong number
Example: Testing Multiple Numbers
Here's a program that tests several numbers to demonstrate strong number identification ?
#include <stdio.h>
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}
int isStrongNumber(int num) {
int sum = 0, temp = num;
while (num > 0) {
int digit = num % 10;
sum = sum + factorial(digit);
num = num / 10;
}
return (sum == temp);
}
int main() {
int numbers[] = {1, 2, 145, 40585, 123};
int size = 5;
for (int i = 0; i < size; i++) {
if (isStrongNumber(numbers[i]))
printf("%d is a strong number<br>", numbers[i]);
else
printf("%d is not a strong number<br>", numbers[i]);
}
return 0;
}
1 is a strong number 2 is a strong number 145 is a strong number 40585 is a strong number 123 is not a strong number
Key Points
- Strong numbers are rare − only a few exist like 1, 2, 145, and 40585.
- The algorithm extracts each digit using modulo 10 and integer division.
- Factorial calculation can be optimized by precomputing factorials 0! to 9!.
Conclusion
A strong number check involves calculating factorials of individual digits and comparing their sum with the original number. This mathematical property makes strong numbers quite rare and interesting to study.
Advertisements
