Java Online Compiler
Example: Strong Number Checker in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// 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(); } }
Output
Clear
ADVERTISEMENTS