Check Strong Number in C++

Check Strong Number in C++ (5 Programs).

IntermediateAdvanced Number ProgramsExample 1 of 13
check-strong-number.cpp
1#include <iostream>
2using namespace std;
3
4int factorial(int n) {
5 if (n == 0 || n == 1) return 1;
6 return n * factorial(n - 1);
7}
8
9bool isStrong(int num) {
10 int original = num;
11 int sum = 0;
12
13 while (num > 0) {
14 int digit = num % 10;
15 sum += factorial(digit);
16 num /= 10;
17 }
18
19 return sum == original;
20}
21
22int main() {
23 int num;
24 cout << "Enter a number: ";
25 cin >> num;
26
27 if (isStrong(num)) {
28 cout << num << " is a Strong number" << endl;
29 } else {
30 cout << num << " is not a Strong number" << endl;
31 }
32
33 return 0;
34}

Output

Enter a number: 145
145 is a Strong number

What's going on

A Strong number is a number where the sum of factorials of its digits equals the number itself. For example, 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145.