-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfectNumber.java
More file actions
30 lines (25 loc) · 826 Bytes
/
Copy pathPerfectNumber.java
File metadata and controls
30 lines (25 loc) · 826 Bytes
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
/***************
* A perfect number is a positive integer equal to the sum of its divisors,
* excluding itself. For instance, 28 is a perfect number because 1 + 2 + 4 + 7 + 14 = 28.
***************/
import java.util.*;
class PerfectNumber{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a number to check either It is perfect or not : ");
int number = input.nextInt();
int sum = 0;
int i=1;
while(i<number){
if(number % i == 0){
sum += i;
}
i++;
}
if(sum == number){
System.out.println(number+" is a perfect number!");
}else{
System.out.println(number+" is NOT a perfect number!");
}
}
}