Strong numbers are special numbers whose sum of the factorials of their digits is equal to the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 145. Learning to check strong numbers in C++ helps beginners practice loops, conditional statements, and mathematical calculations in an engaging way. This article will guide you through multiple methods to check if a number is strong.

with hands-on learning.
get the skills and confidence to land your next move.
Program 1: Basic Method to Check Strong Number
This approach calculates the factorial of each digit using a simple loop and sums them. Then it compares the sum with the original number to determine if it is strong.
#include <iostream>
using namespace std;
int main() {
int n, temp, digit, sum = 0, fact;
cout << "Enter a number: " << endl;
cin >> n;
temp = n;
while(temp > 0) {
digit = temp % 10;
fact = 1;
for(int i = 1; i <= digit; i++)
fact *= i;
sum += fact;
temp /= 10;
}
if(sum == n)
cout << n << " is a strong number." << endl;
else
cout << n << " is not a strong number." << endl;
return 0;
}
In this program, each digit is extracted using the modulus operator and its factorial is calculated using a loop. Summing these factorials and comparing with the original number helps check whether it is strong. Beginners can easily follow this approach to understand nested loops and number manipulations.
Program 2: Using a Factorial Function
Encapsulating factorial calculation inside a function makes the code modular, reusable, and cleaner. This is especially useful when checking multiple numbers.
#include <iostream>
using namespace std;
int factorial(int n) {
int fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
return fact;
}
bool isStrong(int n) {
int temp = n, sum = 0;
while(temp > 0) {
sum += factorial(temp % 10);
temp /= 10;
}
return sum == n;
}
int main() {
int n;
cout << "Enter a number: " << endl;
cin >> n;
if(isStrong(n))
cout << n << " is a strong number." << endl;
else
cout << n << " is not a strong number." << endl;
return 0;
}
This method shows how functions improve readability and avoid code repetition. The factorial()
function can be reused in other programs as well, while the isStrong()
function handles the logic of checking strong numbers, making the program more structured.
Program 3: Optimized Method Using Lookup for Factorials
For numbers with many digits, repeatedly calculating factorials can be inefficient. We can precompute factorials for digits 0–9 and use a lookup to save computation time.
#include <iostream>
using namespace std;
bool isStrongOptimized(int n) {
int factorials[10] = {1,1,2,6,24,120,720,5040,40320,362880};
int temp = n, sum = 0;
while(temp > 0) {
sum += factorials[temp % 10];
temp /= 10;
}
return sum == n;
}
int main() {
int n;
cout << "Enter a number: " << endl;
cin >> n;
if(isStrongOptimized(n))
cout << n << " is a strong number." << endl;
else
cout << n << " is not a strong number." << endl;
return 0;
}
This approach reduces repeated calculations by storing factorials of digits in an array. Beginners can see how small optimizations can make programs faster and more efficient, which is especially important for large numbers.
Frequently Asked Questions (FAQ)
Here are some common questions beginners ask about strong numbers in C++:
Q1: What is a strong number?
A strong number is a number whose sum of the factorials of its digits equals the number itself, like 145 or 1.
Q2: Can a single-digit number be strong?
Yes, numbers 1 and 2 are considered strong because 1! = 1 and 2! = 2.
Q3: Why use a factorial function?
Using a function makes the code modular, reusable, and easier to read, especially when checking multiple numbers.
Q4: Is there a faster way for large numbers?
Precomputing factorials for digits 0–9 and using a lookup array reduces repeated calculations and improves performance.
Q5: Where are strong numbers used?
Strong numbers are mostly mathematical curiosities and exercises, helping beginners practice logic, loops, and functions.
Conclusion
Checking strong numbers in C++ is an excellent way for beginners to practice loops, functions, and optimization. We explored three methods: a basic loop method, a reusable factorial function, and an optimized lookup approach. By experimenting with these methods, beginners can enhance their programming skills and learn to write clean, efficient, and modular code.
Additional & References
Learning about strong numbers helps beginners understand factorials, modular programming, and code optimization. Practice by checking multiple numbers, generating sequences of strong numbers, or combining with other number exercises.
- C++ Reference –
<iostream>
– Documentation for input/output operations. - GeeksforGeeks Strong Number in C++ – Step-by-step explanations with examples.
- Programiz C++ Tutorials – Beginner-friendly C++ tutorials with practice exercises.
- W3Schools C++ – Quick reference and practical coding examples.