Skip to content

Commit 46da767

Browse files
authored
Create AbundantNumber.java
1 parent 6dbb438 commit 46da767

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

AbundantNumber.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.*;
2+
import java.math.*;
3+
4+
// Function to calculate sum of divisors
5+
class GFG{
6+
static int getSum(int n)
7+
{
8+
int sum = 0;
9+
10+
// Note that this loop runs till square
11+
// root of n
12+
for (int i=1; i<=(Math.sqrt(n)); i++)
13+
{
14+
if (n%i==0)
15+
{
16+
// If divisors are equal,take only
17+
// one of them
18+
if (n/i == i)
19+
sum = sum + i;
20+
21+
else // Otherwise take both
22+
{
23+
sum = sum + i;
24+
sum = sum + (n / i);
25+
}
26+
}
27+
}
28+
29+
// calculate sum of all proper divisors
30+
// only
31+
sum = sum - n;
32+
return sum;
33+
}
34+
35+
// Function to check Abundant Number
36+
static boolean checkAbundant(int n)
37+
{
38+
// Return true if sum of divisors is
39+
// greater than n.
40+
return (getSum(n) > n);
41+
}
42+
43+
/* Driver program to test above function */
44+
public static void main(String args[])throws
45+
IOException
46+
{
47+
if(checkAbundant(12))
48+
System.out.println("YES");
49+
else
50+
System.out.println("NO");
51+
if(checkAbundant(15))
52+
System.out.println("YES");
53+
else
54+
System.out.println("NO");
55+
}
56+
}

0 commit comments

Comments
 (0)