Java Online Compiler
Example: Abundant Number Checker in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Abundant Number Checker import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); // Step 2: Prompt the user to enter a number System.out.print("Enter a number to check if it's abundant: "); int number = scanner.nextInt(); // Step 3: Initialize sum of proper divisors int sumOfDivisors = 0; // Step 4: Find proper divisors and sum them for (int i = 1; i <= number / 2; i++) { if (number % i == 0) { sumOfDivisors += i; } } // Step 5: Check if the sum of divisors is greater than the number if (sumOfDivisors > number) { System.out.println(number + " is an abundant number."); } else { System.out.println(number + " is not an abundant number."); } // Step 6: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS