Problem Statement:
Find the abundant integers and number of abundant integers from a given positive integer ‘a’ to a given positive integer ‘b’ where b > a.
Solution:
File Name: Abundant.java
import java.io.*; public class Abundance { static int count = 0; public static void main(String[] args) { int min = Integer.parseInt(getUserInput("Number from which abundant numbers have to printed: ")); int max = Integer.parseInt(getUserInput("Number upto which abundant numbers have to printed: ")); for (int i = min; i <= max; i++) { if (sumOfFactors(i) > i) { System.out.println(i); count++; } } System.out.println("Number of abundant numbers found: " + count); } public static String getUserInput (String prompt) { String inputLine = null; System.out.print(prompt + " "); try { BufferedReader is = new BufferedReader (new InputStreamReader(System.in)); inputLine = is.readLine(); if (inputLine.length() == 0) return null; } catch (IOException e) { System.out.println("IOException" + e); } return inputLine; } public static int sumOfFactors (int i) { int sum = 0; for (int j = 1; j <= i/2; j++) { if (i % j == 0) { sum += j; } } return sum; } }
Exercise:
Solve the above problem yourself. See if you can understand the above solution yourself.