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 check Strong Number
A Strong Number is a number whose sum of the factorials of its digits equals the original number. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
Syntax
int factorial(int digit); int isStrongNumber(int number);
Algorithm
- Extract each digit from the number starting from the unit place
- Calculate the factorial of each digit
- Sum all the factorials
- Compare the sum with the original number
Example
Let's implement a C program to check if a number is a Strong Number −
#include <stdio.h>
int factorial(int r) {
int fact = 1;
while(r > 1) {
fact = fact * r;
r--;
}
return fact;
}
int isStrongNumber(int n) {
int temp, rem, result = 0;
temp = n;
while(temp) {
rem = temp % 10;
result = result + factorial(rem);
temp = temp / 10;
}
if (result == n)
return 1;
else
return 0;
}
int main() {
int n = 145;
printf("Number: %d<br>", n);
if (isStrongNumber(n))
printf("Yes, it is a strong number<br>");
else
printf("No, it is not a strong number<br>");
// Test with another number
n = 124;
printf("\nNumber: %d<br>", n);
if (isStrongNumber(n))
printf("Yes, it is a strong number<br>");
else
printf("No, it is not a strong number<br>");
return 0;
}
Number: 145 Yes, it is a strong number Number: 124 No, it is not a strong number
How It Works
- For 145: 1! + 4! + 5! = 1 + 24 + 120 = 145 (Strong Number)
- For 124: 1! + 2! + 4! = 1 + 2 + 24 = 27 ? 124 (Not a Strong Number)
Key Points
- The factorial function calculates the factorial of a single digit (0-9)
- We extract digits using modulo (%) and integer division (/) operations
- Common strong numbers include: 1, 2, 145, and 40585
Conclusion
A Strong Number is identified by comparing the sum of factorials of its digits with the original number. The algorithm efficiently extracts digits and computes factorials to perform this verification.
Advertisements
