A Strong Number is a number where the sum of the factorials of its digits equals the number itself. For example, the number 145 is a Strong Number because
- The factorial of 1 is 1 (1! = 1)
- The factorial of 4 is 24 (4! = 24)
- The factorial of 5 is 120 (5! = 120)
- The sum is 1 + 24 + 120 = 145, which is equal to the original number.
So, 145 is a Strong Number.
Steps to Check if a Number is a Strong Number
- Extract the digits: Break the number down into its digits.
- Find the factorial of each digit: Calculate the factorial of each digit.
- Add the factorials: Add up the factorials of the digits.
- Compare the sum: If the sum of the factorials is the same as the original number, it is a Strong Number.
Java Program
import java.util.Scanner;
public class StrongNumber {
// Function to calculate the factorial of a number
public static int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
// Function to check if the number is a Strong Number
public static boolean isStrongNumber(int number) {
int originalNumber = number; // Store the original number
int sum = 0; // Store the sum of factorials
// Extract each digit of the number
while (number > 0) {
int digit = number % 10; // Get the last digit
sum += factorial(digit); // Add the factorial of the digit to the sum
number /= 10; // Remove the last digit
}
// Check if the sum equals the original number
return sum == originalNumber;
}
public static void main(String[] args) {
// Create a scanner to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user to input a number
System.out.print("Enter a number: ");
int num = scanner.nextInt();
// Check if the number is a Strong Number
if (isStrongNumber(num)) {
System.out.println(num + " is a Strong Number.");
} else {
System.out.println(num + " is not a Strong Number.");
}
// Close the scanner
scanner.close();
}
}
How does the Program Work?
- Factorial Function: The factorial(int num) method calculates the factorial of a number. It multiplies all numbers from 1 to
- Checking for Strong Number: The isStrongNumber(int number) method checks if the number is a Strong Number. It does this by.
- Extracting each digit of the number using the modulus (%) operator.
- Calculating the factorial of each digit.
- Adding the factorials together.
- Finally, compare the sum with the original number.
- Main Program: In the main() method, the user is asked to enter a number. This number is then checked using the isStrongNumber() method. The program prints whether the number is a Strong Number or not.
Example Outputs
Example 1
- Input: 145
- Output
Example 2
- Input: 123
- Output
Conclusion
A Strong Number is a number where the sum of the factorials of its digits is equal to the number itself. This Java program helps us check whether a number is a Strong Number by breaking the number down into its digits, finding the factorial of each digit, and comparing the sum to the original number. This task is a great way to practice loops, functions, and math operations in Java.