2 Replies - 1491 Views - Last Post: 19 October 2009 - 06:00 PM Rate Topic: -----

#1 pillowpantss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8016
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 pillowpantss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1