-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-04-Java-Q25
More file actions
66 lines (46 loc) · 1.4 KB
/
Day-04-Java-Q25
File metadata and controls
66 lines (46 loc) · 1.4 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
57
58
59
60
61
62
63
64
65
66
/*Veena wants know to whether the given number is perfect number or not.could you please help her to find it and implements in program.
If the Sum of proper factor is equals to the given number,its a perfect number.
For example,Proper factor of 6 is 1,2,3=>(1+2+3)=6
sum of factor is 6 and the given number is also 6 so its a perfect number.
Input Format
integer consists of one integer.
Constraints
Given N is Non Negative Numbers
Output Format
Execute the statement "Perfect Number" or "Not Perfect Number".
Sample Input 0
6
Sample Output 0
The Number 6.0 is a Perfect Number.
Sample Input 1
10
Sample Output 1
The Number 10.0 is a Not Perfect Number.
Sample Input 2
-1
Sample Output 2
Invalid Input*/
#Answer
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n < 0) {
System.out.println("Invalid Input");
return;
}
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum == n && n != 0) {
System.out.println("The Number " + (double)n + " is a Perfect Number.");
} else {
System.out.println("The Number " + (double)n + " is a Not Perfect Number.");
}
}
}