# Perfect number program help

Page 1 of 1

## 2 Replies - 1491 Views - Last Post: 19 October 2009 - 06:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133066&amp;s=8bc5d9ca153494b19bcf4385a6dc2f91&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pillowpantss

Reputation: 0
• Posts: 4
• Joined: 19-October 09

# Perfect number program help

Posted 19 October 2009 - 05:22 PM

I've spent hours and hours on this project, which is finally due tonight and do to last resorts I've looked for help on here. I've already searched the other perfect number post but none seem to help my cause. I have to make a program where a user enters a postive integer, and the program will tell the user if it is perfect or not. We also have to add what numbers are in that perfect number. For example: 28 = 1 + 2 + 4 + 7 +14. This simply has me stumped, any help is very much welcomed, here is the code:

package perfectnumber;

import java.util.*;
public class PefectChecker {

public static void main(String[] args) {

System.out.println("Enter a perfect number greater than 0");

Scanner input = new Scanner(System.in);
int number = input.nextInt();

int sum = 0;
for(int divisor = 1; divisor < number; divisor++){
if((number % divisor) ==0){
sum += divisor;
if(sum < number){
System.out.print(sum + " + ");
}
else{
System.out.print(" = " + number);
}
}
}

if(number == 0){
System.out.println(number + " is not a perfect number." );
}
else if(sum == number){
System.out.println("");
System.out.println(number + " is a perfect number." );
}
else{
System.out.println(number + " is not a perfect number." );
}

}
}

Is This A Good Question/Topic? 0

## Replies To: Perfect number program help

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8016
• Posts: 31,118
• Joined: 06-March 08

## Re: Perfect number program help

Posted 19 October 2009 - 05:32 PM

I am not a perfect number expert but

it is find to test that this is a perfect multiplier

if((number % divisor) ==0){
sum += divisor;

but if it is the case I guess that you have to update your number

if((number % divisor) ==0){
sum += divisor;
number /= divisor;

and then I guess you have to restart your loop starting back to 2

This post has been edited by pbl: 19 October 2009 - 05:32 PM

### #3 pillowpantss

Reputation: 0
• Posts: 4
• Joined: 19-October 09

## Re: Perfect number program help

Posted 19 October 2009 - 06:00 PM

Just wanted to reply that I found a way to make it work.
import java.util.*;
public class PefectChecker {

public static void main(String[] args) {

//declare global variables
int sum = 0;

//prompt user for input
System.out.print("Enter a perfect number. ");
Scanner input = new Scanner(System.in);
int number = input.nextInt();

//determine if input is a perfect number
for(int divisor = 1; divisor <= number/2; divisor++){
if(number % divisor ==0){
sum = divisor + sum;
}
}

//If prime, determine if input is valid or invalid and display prime result.
if(number <= 0){
System.out.println(number + " is an invalid number  ." );
}
else if(sum == number){
System.out.println("");
System.out.println(number + " is a perfect number." );
System.out.print(number+ " = ");
//Print the numbers that add up to get the prime number
for(int divisor = 1; divisor <= number/2; divisor++){
if(number % divisor ==0){
sum = divisor + sum;
if(divisor == number/2){
System.out.print(divisor);
}
else{
System.out.print(divisor + " + ");
}
}
}
}
else{
System.out.println(number + " is not a perfect number." );
}
}
}

This post has been edited by pillowpantss: 19 October 2009 - 06:09 PM