-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathPerfectNumber.java
40 lines (32 loc) · 1.09 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
// Java program to check if the number is an Perfect number or not
import java.util.Scanner;
public class PerfectNumber {
public static void main(String[] args) {
// take user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int N = sc.nextInt();
// initialize variable for loop and to store sum
int i, sum = 0;
// iterate from 1 to N and if the number is a factor of N then add it to sum
for (i = 1; i < N; i++) {
if (N % i == 0) {
sum += i;
}
}
// if sum of factors of N is equal to N then it is a perfect number
if (sum == N) {
System.out.print(N + " is a Perfect Number\n");
} else {
System.out.print(N + " is not a Perfect Number\n");
}
}
}
/*******************************************************
Case 1:
Enter a number: 153
153 is not a Perfect number
Case 2:
Enter a number : 496
496 is a Perfect Number
*******************************************************/