This is a program that finds all perfect numbers that exist from 1 through 50,00
ID: 3629822 • Letter: T
Question
This is a program that finds all perfect numbers that exist from 1 through 50,000. The problem i'm having is that whenever I run the program it only prints the number 3. I assume there's a logical error somewhere..but i can't find it. Can you see what is wrong?
In case you dont know a perfect number is a number that is the sum of its divisors. so for example:
6 is a perfect number because its divisors 1+2+3 = 6
28 is a perfect number because its divisors 1+2+4+7+14=28
//------------------------------------code----------------------------
import java.io.*;
import java.util.Scanner;
///////////////////////////////////////////////////
class Hw04
{
//-----------------------------------------------------------------------
public static void main ( String [] args ) throws Exception
{
for ( int i = 1 ; i <= 50000 ; i++ )
if ( isPerfect(i) ) System.out.println(i);
}
//-----------------------------------------------------------------------
public static boolean isPerfect ( int n )
{
boolean perfectNum;
int sum = 0;
for (int divider = 1; divider < n; divider++)
{
if( n % divider == 0);
sum += divider;
if (sum == n)
perfectNum = true;
}
return perfectNum;
}
//-----------------------------------------------------------------------
}
Explanation / Answer
import java.io.*; import java.util.Scanner; /////////////////////////////////////////////////// class Hw04 { //----------------------------------------------------------------------- public static void main ( String [] args ) throws Exception { for ( int i = 1 ; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.