forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbundantNumber.java
More file actions
56 lines (50 loc) · 1.37 KB
/
AbundantNumber.java
File metadata and controls
56 lines (50 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.math.*;
// Function to calculate sum of divisors
class AbundantNumber{
static int getSum(int n)
{
int sum = 0;
// Note that this loop runs till square
// root of n
for (int i=1; i<=(Math.sqrt(n)); i++)
{
if (n%i==0)
{
// If divisors are equal,take only
// one of them
if (n/i == i)
sum = sum + i;
else // Otherwise take both
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
// calculate sum of all proper divisors
// only
sum = sum - n;
return sum;
}
// Function to check Abundant Number
static boolean checkAbundant(int n)
{
// Return true if sum of divisors is
// greater than n.
return (getSum(n) > n);
}
/* Driver program to test above function */
public static void main(String args[])throws
IOException
{
if(checkAbundant(12))
System.out.println("YES");
else
System.out.println("NO");
if(checkAbundant(15))
System.out.println("YES");
else
System.out.println("NO");
}
}