Check Strong Number

Check Strong Number in C++

C++Intermediate
C++
#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

Check Strong Number in C++

This program teaches you how to check if a number is a Strong number in C++. A Strong number is a special number where the sum of factorials of its digits equals the number itself. This program demonstrates how to break down a number into its digits, calculate factorials, and verify if the number meets the Strong number criteria.

What is a Strong Number?

A Strong number is a number where the sum of factorials of its digits equals the number itself.

Example:

  • 145 is a Strong number because:
    • 1! + 4! + 5! = 1 + 24 + 120 = 145
  • 40585 is also a Strong number:
    • 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585

Algorithm

  1. Extract each digit from the number
  2. Calculate the factorial of each digit
  3. Sum all the factorials
  4. Compare the sum with the original number
  5. If they match, it's a Strong number

Understanding Factorials

Factorial of a number n (written as n!) is the product of all positive integers from 1 to n:

  • 0! = 1 (by definition)
  • 1! = 1
  • 2! = 2 × 1 = 2
  • 3! = 3 × 2 × 1 = 6
  • 4! = 4 × 3 × 2 × 1 = 24
  • 5! = 5 × 4 × 3 × 2 × 1 = 120

Step-by-Step Process

For number 145:

  1. Extract digits: 1, 4, 5
  2. Calculate factorials:
    • 1! = 1
    • 4! = 24
    • 5! = 120
  3. Sum: 1 + 24 + 120 = 145
  4. Compare: 145 == 145 ✓ (Strong number)

Summary

  • A Strong number is where sum of digit factorials equals the number.
  • Extract digits using modulo (%) and division (/) operations.
  • Calculate factorial for each digit (recursive or iterative).
  • Sum all factorials and compare with original number.
  • This program combines digit extraction, factorial calculation, and comparison logic.

This program is excellent for learning digit manipulation, factorial calculation, and number theory concepts in C++.