Check Strong Number in C++
Check for a strong number in C++, where the factorials of the digits add up to the number itself. Traces 145 step by step and explains the factorial helper.
1#include <iostream>2using namespace std;34int factorial(int n) {5 if (n == 0 || n == 1) return 1;6 return n * factorial(n - 1);7}89bool isStrong(int num) {10 int original = num;11 int sum = 0;1213 while (num > 0) {14 int digit = num % 10;15 sum += factorial(digit);16 num /= 10;17 }1819 return sum == original;20}2122int main() {23 int num;24 cout << "Enter a number: ";25 cin >> num;2627 if (isStrong(num)) {28 cout << num << " is a Strong number" << endl;29 } else {30 cout << num << " is not a Strong number" << endl;31 }3233 return 0;34}
Sample Output
145 is a Strong number
1! + 4! + 5! = 145.
In this one
- What a strong number is
- How a recursive factorial function stops at its base case
- How % 10 and / 10 split a number into digits
- How a bool function returns a comparison directly
How it works
- 1
Read num (145).
- 2
isStrong saves original and starts sum at 0.
- 3
For each digit 5, 4, 1, add factorial(digit).
- 4
sum reaches 145.
- 5
sum == original is true, so main prints the Strong message.
isStrong(145)
sum and num after each digit.
- 1.digit 5: 5! = 120, sum 120, num 14
- 2.digit 4: 4! = 24, sum 144, num 1
- 3.digit 1: 1! = 1, sum 145, num 0
Edge cases
Input 0
Enter 0.
The loop never runs, so sum stays 0 and 0 is reported as strong, even though 0! is 1. Add a special case if needed.
Negative input
Enter -145.
while (num > 0) is false at once, sum is 0, and 0 != -145, so it prints not strong.
What's going on
A strong number equals the sum of the factorials of its own digits. 145 is the classic one: 1! + 4! + 5! = 1 + 24 + 120 = 145. This program splits the job into two small functions, which is a good habit to copy.
int factorial(int n)
A recursive helper. If n is 0 or 1 it returns 1. That is the base case: the point where the function stops calling itself. Otherwise it returns n * factorial(n - 1). Each call waits on the call stack for the smaller one to finish, then multiplies on the way back. Digits only go up to 9, and 9! is 362880, so int is plenty here. Remove the base case and the calls never stop until the stack runs out of space and the program crashes.
bool isStrong(int num)
original keeps the input, because the loop is about to chew num down to 0.
while (num > 0)
int digit = num % 10; peels off the last digit.
sum += factorial(digit); adds that digit's factorial.
num /= 10; drops the digit.
return sum == original;
The comparison itself is the answer, true or false. No if/else needed inside the function.
Trace for 145:
main reads a number, calls isStrong, and prints one of two messages.
Try 123: 1 + 2 + 6 = 9, not strong. Try 2: 2! = 2, strong. 1 is strong too (1! = 1).
Zero is a quiet trap. while (num > 0) never runs for 0, so sum stays 0 and 0 == 0 reports 0 as strong. By the usual definition 0! = 1, so 0 should not be strong. If your teacher cares, handle 0 before the loop.
There are only four strong numbers in base 10: 1, 2, 145, and 40585. A faster version would precompute the ten digit factorials in an array instead of recursing every time. For one input it does not matter; for checking a million numbers it does.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Is 40585 strong?
Pick an answer to check it.
Question 2
What does factorial(0) return here?
Pick an answer to check it.
Questions people ask
Which numbers are strong?
In base 10 only 1, 2, 145, and 40585.
Why use recursion for factorial?
It mirrors the math definition n! = n × (n−1)!. A loop works equally well.
Could the factorial overflow?
Not here, since digits are 0–9 and 9! = 362880 fits in int.