-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperfectn.java
45 lines (42 loc) · 938 Bytes
/
perfectn.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
import java.util.Scanner;
public class perfectn
{
public static void main(String args[])
{
try
{
long n, sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
n=sc.nextLong();
int i=1;
if(n<=0)
{
System.out.println("Enter the valid number ");
}
else
{
while(i <= n/2)
{
if(n % i == 0)
{
sum = sum + i;
}
i++;
}
if(sum==n)
{
System.out.println(n+" is a perfect number.");
}
else
{
System.out.println(n+" is not a perfect number.");
}
}
}
catch(Exception e)
{
System.out.print("Enter the valid number");
}
}
}