In the previous article, we have discussed Java Program to Check Xylem and Phloem Number
In this article we are going to understand what Abundant number is and how we can check whether a number is Abundant or not in Java with examples.
Program to Check Abundant Number
Abundant numbers are numbers whose sum of all proper divisors is more than the number.
Example : Abundant Number: 12: Divisors- 1,2,3,4,6; Sum = 1+2+3+4+6 = 16>12 40: Divisors- 1,2,4,5,8,10,20; Sum = 1+2+4+5+8+10+20 = 50>40 Not Abundant Number: 15: Divisors- 1,3,5; Sum = 1+3+5 = 9<15
In the above examples the numbers 15 is not abundant numbers as sum of its divisors are lesser than the number itself. Hence 12,40 are Abundant numbers.
Let’s see different ways to check abundant number.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach :
- Enter/Declare a number and store it .
- Then we find the divisors of the number and add them up.
- If the resultant sum is greater than the number itself, then the number is said to be an Abundant number.
Method-1: Java Program to Check Abundant Number By Using Static Value
import java.util.Scanner; public class AbundantNumber { public static void main(String args[]) { //A n number declared; int num = 40; int sum = 0; // Loop to find the divisors as well as add them up for(int i = 1; i<num; i++) if(num%i==0) sum+=i; if(sum>num) { System.out.println(num+" is an Abundant number"); } else { System.out.println(num+" is Not an Abundant number"); } } }
Output: 40 is an Abundant number
Method-2: Java Program to Check Abundant Number By User Input Value
import java.util.Scanner; public class AbundantNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int sum = 0; // Loop to find the divisors as well as add them up for(int i = 1; i<num; i++) if(num%i==0) sum+=i; if(sum>num) { System.out.println(num+" is an Abundant number"); } else { System.out.println(num+" is Not an Abundant number"); } } }
Output: Case-1 Enter a number : 40 40 is an Abundant number Case-2 Enter a number : 15 15 is Not an Abundant number
Method-3: Java Program to Check Abundant Number By User Defined Method
import java.util.Scanner; public class AbundantNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //checkNumber() method called to check Abudant number checkNumber(num); } public static void checkNumber(int num) { int sum = 0; // Loop to find the divisors as well as add them up for(int i = 1; i<num; i++) if(num%i==0) sum+=i; if(sum>num) { System.out.println(num+" is an Abundant number"); } else { System.out.println(num+" is Not an Abundant number"); } } }
Output: Case-1 Enter a number : 40 40 is an Abundant number Case-2 Enter a number : 15 15 is Not an Abundant number
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: