-
Notifications
You must be signed in to change notification settings - Fork 109
/
PerfectNumber.java
50 lines (48 loc) · 1.07 KB
/
PerfectNumber.java
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
/* Check a number is perfect or not
* Input : 6
* Output : Perfect Number
*
* Explaination
* --------------------------
* The divisors of 6 = 1,2,3. 1+2+3 = 6
* Sum of the factors is equal to the given number. So this is perfect
*
* Input : 8
* Output : Not Perfect Number
*
* Explaination
* -------------------------
* Factors of 8 = 1,2,4, but 1+2+4 = 7 not equal to 8.
*/
import java.util.*;
public class PerfectNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean check = isPerfect(n);
if(check == true)
{
System.out.println("Perfect Number");
}
else
{
System.out.println("Not Perfect Number");
}
}
public static boolean isPerfect(int n)
{
int sum = 0;
for (int i = 1; i < n; i++)
{
if(n % i == 0)
{
sum += i;
}
}
if(sum == n)
{
return true;
}
return false;
}
}