A number is called a proper divisor of N if M < N and M divides N. A positive integer is called perfect if it is the sum of its positive proper divisors. For example, the positive proper divisors of 28 are 1, 2, 4, 7, and 14 and 1+2+4+7+14=28. Therefore, 28 is perfect. Write a program to display the first 4 perfect integers.
I got this but the loop won't stop running.
public class PerfectNumbers
{
static int FIRSTPERFECT;
static int SECONDPERFECT;
static int THIRDPERFECT;
static int FOURTHPERFECT;
public static void main(String[] args)
{
findPerfect();
System.out.println(FIRSTPERFECT);
System.out.println(SECONDPERFECT);
System.out.println(THIRDPERFECT);
System.out.println(FOURTHPERFECT);
}
public static void findPerfect()
{
int count = 0;
int sum = 0;
int n = 1;
while(count <= 4)
{
int divisor = 1;
n = 1;
sum = 0;
while (divisor < n)
{
if(n % divisor == 0)
{
sum += divisor;
}
divisor++;
}
if(sum == n)
{
if(count == 1)
{
FIRSTPERFECT = n;
count++;
n++;
}
else if(count == 2)
{
SECONDPERFECT= n;
count++;
n++;
}
else if(count == 3)
{
THIRDPERFECT = n;
count++;
n++;
}
else if(count == 4)
{
FOURTHPERFECT = n;
count++;
n++;
}
}
else
n++;
}
}
}
2
Contributors
1
Reply
8 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
Related Article:Read numbers from scanner
is a solved Java discussion thread by trishtren that has 2 replies, was last updated 10 months ago and has been tagged with the keywords: scanner, numbers, stuff.