Program to Check if a Number is an Abundant Number in Java
Abundant Number
An abundant number is a number for which the sum of its proper divisors is greater than the number itself. For example, 12 is an abundant number because its divisors (1, 2, 3, 4, 6) sum to 16, which is greater than 12.
We will explore a method to check if a given number is an abundant number using Java programming.
Method: Using a for Loop
We iterate through all numbers less than the given number to find its divisors and sum them up.
import java.util.Scanner; public class Main { public static boolean isAbundant(int num) { int sum = 0; // Calculate the sum of proper divisors for (int i = 1; i <= num / 2; i++) { if (num % i == 0) { sum += i; } } return (sum > num); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int num = scanner.nextInt(); if (isAbundant(num)) System.out.println(num + " is an Abundant number"); else System.out.println(num + " is not an Abundant number"); scanner.close(); } }
Output:
Enter a number: 12 12 is an Abundant number