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;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}
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.