Strong Number Or Not In Java Program
A strong number is a number whose sum of the factorial of its digits equals the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145. In this article, you will learn how to determine if a given number is a strong number using a Java program.
Problem Statement
The task is to write a Java program that takes an integer as input and checks whether it is a strong number or not. This involves calculating the factorial of each digit and summing them up, then comparing the sum with the original number.
Example
If the input number is 145, the program should output that 145 is a strong number.
Background & Knowledge Prerequisites
To understand this program, you should be familiar with:
- Basic Java syntax (variables, data types, loops, conditional statements).
- Arithmetic operators (modulo
%and division/). - Methods and method calls.
Use Cases or Case Studies
- Educational Tool: Helps students understand digit manipulation and factorial calculations.
- Number Theory Exploration: Used in programs exploring properties of numbers.
- Coding Challenges: A common problem in introductory programming contests.
- Algorithm Practice: Good for practicing basic algorithmic thinking.
Solution Approaches
We will implement a single, straightforward approach to solve this problem.
Approach 1: Iterative Digit Factorial Sum
This approach involves extracting each digit of the number, calculating its factorial, and summing these factorials.
// Strong Number Checker
import java.util.Scanner;
// Main class containing the entry point of the program
public class Main {
// Method to calculate the factorial of a number
public static long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
long fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
// Step 1: Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Step 2: Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Step 3: Store the original number for comparison later
int originalNumber = number;
long sumOfFactorials = 0;
// Step 4: Iterate through each digit of the number
while (number > 0) {
int digit = number % 10; // Get the last digit
sumOfFactorials += factorial(digit); // Add factorial of the digit to the sum
number /= 10; // Remove the last digit
}
// Step 5: Compare the sum of factorials with the original number
if (sumOfFactorials == originalNumber) {
System.out.println(originalNumber + " is a strong number.");
} else {
System.out.println(originalNumber + " is not a strong number.");
}
// Step 6: Close the scanner
scanner.close();
}
}
Sample Output
Enter a number: 145
145 is a strong number.
Enter a number: 123
123 is not a strong number.
Stepwise Explanation
factorial(int n)Method:
- This helper method calculates the factorial of a given integer
n. - It handles base cases where
nis 0 or 1, returning 1. - For other numbers, it iteratively multiplies numbers from 2 up to
nto compute the factorial.
- Input Reading:
- A
Scannerobject is created to read an integer input from the user.
- Initialization:
- The input
numberis stored inoriginalNumberbecause thenumbervariable will be modified during digit extraction. -
sumOfFactorialsis initialized to 0 to accumulate the sum of factorials of digits.
- Digit Extraction and Factorial Summation:
- A
whileloop continues as long asnumberis greater than 0. -
number % 10extracts the last digit of the number. - The
factorial()method is called with this digit, and its result is added tosumOfFactorials. -
number /= 10removes the last digit from the number, effectively shifting the number to the right by one decimal place.
- Comparison and Output:
- After the loop finishes,
sumOfFactorialsholds the sum of factorials of all digits. - This sum is compared with
originalNumber. - An appropriate message is printed indicating whether the number is strong or not.
- Resource Cleanup:
- The
scanner.close()method is called to release system resources.
Conclusion
Determining if a number is a strong number involves a straightforward process of digit extraction, factorial calculation, and summation. The provided Java program effectively implements this logic using a helper method for factorial calculation and an iterative approach to process each digit.
Summary
- A strong number is one where the sum of the factorials of its digits equals the number itself.
- The program uses a
factorial()helper method to compute factorials efficiently. - Digits are extracted using the modulo (
%) and division (/) operators within awhileloop. - The sum of these factorials is then compared to the original number to determine if it's a strong number.