Program to check whether a number is Strong number or not

Write a program that takes an integer as input and checks whether it is a Strong Number.
A Strong Number is a number in which the sum of the factorial of its digits is equal to the number itself. For example, 145 is a Strong Number because 1! + 4! + 5! = 145.

Input/Output Examples

  • Input: 145
    Output: 145 is a Strong Number.
  • Input: 123
    Output: 123 is not a Strong Number.

Algorithm to check whether a number is Strong number or not

  1. Start.
  2. Read the number n.
  3. Save the original value of n to compare later.
  4. Initialize sum to 0.
  5. Repeat until n is greater than 0:
    • Find the last digit of n by n % 10.
    • Compute the factorial of the digit.
    • Add the factorial to sum.
    • Remove the last digit from n by dividing it by 10.
  6. If sum is equal to the original number, it is a Strong Number.
  7. Display the result.
  8. End.

C++ program to check whether a number is Strong number or not

cpp
#include<iostream>
using namespace std;

// Function to calculate factorial
int factorial(int num) {
    int fact = 1;
    for(int i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

int main() {
    int n, sum = 0, originalNum, digit;
    cout << "Enter a number: ";
    cin >> n;
    originalNum = n;

    // Calculate the sum of the factorial of digits
    while(n > 0) {
        digit = n % 10; // Get the last digit
        sum += factorial(digit); // Add factorial of digit to sum
        n /= 10; // Remove the last digit
    }

    // Check if sum of factorials is equal to the original number
    if(sum == originalNum) {
        cout << originalNum << " is a Strong Number.";
    } else {
        cout << originalNum << " is not a Strong Number.";
    }
    return 0;
}

Java program to check whether a number is Strong number or not

java
import java.util.Scanner;

public class StrongNumber {
    // Function to calculate factorial
    public static int factorial(int num) {
        int fact = 1;
        for(int i = 1; i <= num; i++) {
            fact *= i;
        }
        return fact;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = scanner.nextInt();
        int sum = 0, originalNum = n, digit;

        // Calculate the sum of the factorial of digits
        while(n > 0) {
            digit = n % 10; // Get the last digit
            sum += factorial(digit); // Add factorial of digit to sum
            n /= 10; // Remove the last digit
        }

        // Check if sum of factorials is equal to the original number
        if(sum == originalNum) {
            System.out.println(originalNum + " is a Strong Number.");
        } else {
            System.out.println(originalNum + " is not a Strong Number.");
        }
    }
}

Python program to check whether a number is Strong number or not

python
# Function to calculate factorial
def factorial(num):
    if num == 0:
        return 1
    fact = 1
    for i in range(1, num + 1):
        fact *= i
    return fact

# Read input from user
n = int(input("Enter a number: "))
originalNum = n
sum = 0

# Calculate the sum of the factorial of digits
while n > 0:
    digit = n % 10 # Get the last digit
    sum += factorial(digit) # Add factorial of digit to sum
    n //= 10 # Remove the last digit

# Check if sum of factorials is equal to the original number
if sum == originalNum:
    print(f"{originalNum} is a Strong Number.")
else:
    print(f"{originalNum} is not a Strong Number.")
tools

Computer Science

Related Articles