Abundant number or not using Java

Abundant number or not using java :

An abundant number is a number for which the sum of its proper factors is greater than the number itself. For better understanding lets consider an example so that we can easily understand that exactly what is an abundant number.

Abundant number is also known as Excessive number. 

Example :

Suppose an integer value is 24 , then its proper factors are 1, 2, 3, 4, 6, 8, and 12, whose sum is 36. Because 36 is more than 24, the number 24 is abundant. Its abundance is 36 − 24 = 12.

Abundant number or not using java

Working :

Step 1 : Ask the user to enter an integer.

Step 2 : Declare a variable to store sum of factors of the number.

Step 3 : Use a loop to find factors of the numbers and calculate sum of those factors and store the resultant.

Step 4 : Compare that whether the sum of factors of the integer is greater than the given integer or not.

  • If this gets true , then the given number is an Abundant Number.
  • Else the number is not an Abundant Number. 

Code in Java :

//Java program to check whether a number is abundant number or not
import java.util.Scanner;
public class abundant_number_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//declare a variable to store sum of factors of the number
int sum = 0;
//loop for calculating sum of factors of the number
for(int i = 1 ; i < number ; i++)
{
if(number % i == 0)
sum = sum + i;
}
//condition for checking whether the sum is greater than number or not
if(sum > number)
System.out.println("Abundant Number");
else
System.out.println("Not an Abundant Number");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}

Output :

Enter a number : 12
Abundant Number

Enter a number : 15
Not an Abundant Number