Check Strong Number
Check Strong Number in C++ (5 Programs)
IntermediateTopic: Advanced Number Programs
C++ Check Strong Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}
bool isStrong(int num) {
int original = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
return sum == original;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isStrong(num)) {
cout << num << " is a Strong number" << endl;
} else {
cout << num << " is not a Strong number" << endl;
}
return 0;
}Output
Enter a number: 145 145 is a Strong number
Understanding Check Strong Number
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. This program demonstrates 5 different methods to check for strong numbers.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.