Abundant Number in JAVA || BluejCode
Abundant Number in JAVA
Abundant
Number: A
number ‘n’ is said to be Abundant Number if sum of all the proper
divisors of the number is greater than the number ‘n’.
The difference between Sum & Number is called the abundance.
The abundant number can be called as an excessive number
Example :-
Input : 12
Proper Divisors : 1, 2, 3, 4, 6
Sum of Divisors : 16
Output : It is Abundant Number
Abundance : 16 – 12 = 4
Input : 21
Proper Divisors : 1, 3, 7
Sum of Divisors : 11
Output : It is not Abundant Number
Abundance : No abundance
Algorithm :-
Input number
Store the sum of proper divisors in a variable
Check if the sum is Greater than the number or not
If sum is greater than print “It is Abundant Number”
Also print its abundance by subtracting number from sum.
Otherwise, Print “It is not an Abundant Number”
Compile and Run the Program.
Program :-
import java.io.*;
import java.util.*;
class Abundant_Number
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int n,i,sum=0,x;
System.out.println("Enter the number");
n= in.nextInt(); // Enter Number from user
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum>n)
{
System.out.println("It is Abundant Number : "+n);
x = sum-n;
System.out.println("Abundance Number : "+x);
}
else
{
System.out.println("It is not an Abundant Number : "+n);
}
}// end of main
}//end of class
Comments
Post a Comment