# Perfect number program help PLEASE

Page 1 of 1

## 14 Replies - 1293 Views - Last Post: 28 October 2014 - 09:47 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=356886&amp;s=3fa3809a6b58bd4063a9930fc51c1eb8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

# Perfect number program help PLEASE

Posted 28 October 2014 - 07:10 PM

```import java.util.Scanner;
public class pdonahue_Perfect
{
public static void main (String args[])
{
int test;
int value=0;
Scanner input = new Scanner(System.in);

do
{
System.out.print("How many numbers would you like to test? ");
test = input.nextInt();
}
while(test<1);

// int counter = 0;
do
{
System.out.print("Please enter a possible perfect number: ");
value = input.nextInt();
testPerfect(value);
printFactors(value);
// counter++;
}
while(value<test);
}

public static boolean testPerfect(int value)
{
int sum=0;
int test=0;
for(int i = 0; i < test ; i++)
{
if(value % i==0)
{
sum += i;
}
}
if(sum==value)
{
return true;
}
else
{
return false;
}
}
public static void printFactors(int value)
{
int x;
int sum=0;
//  int factor;

Scanner input= new Scanner(System.in);

for(int factor=1; factor < value; factor++)
{
x = value % factor;

if (x==0)
{
sum = sum + factor;
}
}
if (sum!= value)
{
System.out.printf("%d:NOT PERFECT",value);
}

if(sum==value)
{
System.out.printf("%d: ",value);

for(int factor=1; factor < value; factor++)
{
x=value%factor;
if(x==0)
{
System.out.printf("%d ",factor);
}
}
}
System.out.print("\n");
sum=0;
}
}
```

i have just a couple of issues when running my program.
-First, When i enter 0 it should print..

Please enter a possible perfect number: 0
0:NOT PERFECT

-Second, when i get a perfect number. it should show the answer in descending order..

Please enter a possible perfect number: 6
6:3 2 1

mine shows:
Please enter a possible perfect number: 6
6: 1 2 3

**everything else compiles correctly with no errors

Thanks!!

Is This A Good Question/Topic? 0

## Replies To: Perfect number program help PLEASE

### #2 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 07:48 PM

when i put 0 in for my input i get
Please enter a possible perfect number: 0
0:

This is incorrect

### #3 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 07:58 PM

okay now i just have the 0 problem. I fixed the other issue.

### #4 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:08 PM

ill post my revised code

### #5 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:17 PM

```import java.util.Scanner;
public class pdonahue_Perfect
{
public static void main (String args[])
{
int test;
int value=0;
Scanner input = new Scanner(System.in);

do
{
System.out.print("How many numbers would you like to test? ");
test = input.nextInt();
}
while(test<1);

// int counter = 0;
do
{
System.out.print("Please enter a possible perfect number: ");
value = input.nextInt();
testPerfect(value);
printFactors(value);
// counter++;
}
while(value<test);
}

public static boolean testPerfect(int value){
int sum=0;
int test=0;
for(int i = 0; i < test ; i++)
{
if(value % i==0)
{
sum += i;
}
}
if(sum==value)
{
return true;
}
else
{
return false;
}
}
public static void printFactors(int value){
int x;
int sum=0;
//    int factor;

Scanner input= new Scanner(System.in);

for(int factor = value - 1; factor >0; factor--)
{
x = value % factor;

if (x==0)
{
sum = sum + factor;
}
}
if (sum!= value)
{
System.out.printf("%d:NOT PERFECT",value);
}

if(sum==value)
{
System.out.printf("%d: ",value);

for(int factor = value - 1; factor > 0; factor--)
{
x=value%factor;
if(x==0)
{
System.out.printf("%d ",factor);
}
}
}
System.out.print("\n");
sum=0;
}
}

```

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12219
• Posts: 45,292
• Joined: 27-December 08

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:29 PM

### #7 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:34 PM

dang-it, i tried to line up the curly brackets from the open to close. and i made sure to put the code tags around correctly also. i'm trying to indent as you would like. just going to take more practice.

I just found another issue..

whenever i input 16. it stops my loop.
How many numbers would you like to test? 10
Please enter a possible perfect number: 16
16:NOT PERFECT
***then it starts all over, even though i should be able to input 9 more inputs.

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12219
• Posts: 45,292
• Joined: 27-December 08

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:42 PM

This really isn't rocket science to format your code.
```    do
{
System.out.print("How many numbers would you like to test? ");
test = input.nextInt();
}
while(test<1);

```

Should really be formatted as:
```    do {
System.out.print("How many numbers would you like to test? ");
test = input.nextInt();
}while(test<1);

```

Once you have properly formatted code, I will be happy to take a look. But I'm not going to try and weed through the mess you've posted, especially since I've taken the time in two of your other threads to explain in detail good practices. Please meet us halfway.

### #9 infernorthor

• D.I.C Lover

Reputation: 362
• Posts: 1,718
• Joined: 07-February 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:44 PM

you should put your code in at least a text editor to get proper indentation and copy it over

You shouldn't be surprised of the output, read the code you wrote!
while(value<test);

### #10 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:49 PM

```import java.util.Scanner;
public class pdonahue_Perfect {
public static void main(String args[]) {
int test;
int value = 0;
Scanner input = new Scanner(System.in);

do {
System.out.print("How many numbers would you like to test? ");
test = input.nextInt();
}
while (test < 1);

int counter = 0;
do {
System.out.print("Please enter a possible perfect number: ");
value = input.nextInt();
testPerfect(value);
printFactors(value);
counter++;
}
while (value < test);
}

public static boolean testPerfect(int value) {
int sum = 0;
int test = 0;
for (int i = 0; i < test; i++) {
if (value % i == 0) {
sum += i;
}
}
if (sum == value) {
return true;
} else {
return false;
}
}
public static void printFactors(int value) {
int x;

int sum = 0;
//    int factor;

Scanner input = new Scanner(System.in);

for (int factor = 1; factor < value; factor++) {
x = value % factor;

if (x == 0) {
sum = sum + factor;
}
}
if (sum != value) {
System.out.printf("%d:NOT PERFECT", value);
}

if (sum == value) {
System.out.printf("%d: ", value);

for (int factor = value - 1; factor > 0; factor--) {
x = value % factor;
if (x == 0) {
System.out.printf("%d ", factor);
}
}
}
System.out.print("\n");
sum = 0;
}
}
```

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12219
• Posts: 45,292
• Joined: 27-December 08

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:51 PM

Perfect! That really looks a lot better. Thank you for cleaning up your code. And in your second loop, I think you want counter < test, not value < test.

### #12 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:53 PM

im glad i got that correct now lol

i will change it and try it now.

### #13 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12219
• Posts: 45,292
• Joined: 27-December 08

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:55 PM

And it's an important skill to have correct! Better to make a few folks on the internet happy now, than to piss your coworkers off in a few years when they can't make heads or tails of your code. And yes- they will be working with your code (and vice versa).

### #14 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 09:13 PM

thank you very much!! that got rid of the loop ending etc. However, when i input 0 it says:

Please enter a possible perfect number: 0
0:

but, its supposed to say:

Please enter a possible perfect number: 0
0:NOT PERFECT

Thats the last issue i am having.

### #15 pdonahue

Reputation: -1
• Posts: 67
• Joined: 26-September 14

## Re: Perfect number program help PLEASE

Posted 28 October 2014 - 09:47 PM

its something with my if statements at the very end method. i changed the last method to this...

```public static void printFactors(int value) {
int x;
int sum = 0;

Scanner input = new Scanner(System.in);

for (int factor = 1; factor < value; factor++) {
x = value % factor;

if (x == 0) {
sum = sum + factor;
}
}
if (sum != value) {
System.out.printf("%d:NOT PERFECT", value);
}
if (sum == 0){
System.out.printf("%d:NOT PERFECT", value);
}
if (sum == value || sum != 0) {
System.out.printf("%d: ", value);

for (int factor = value - 1; factor > 0; factor--) {
x = value % factor;
if (x == 0) {
System.out.printf("%d ", factor);
}
}
}
System.out.print("\n");
sum = 0;
}
}

```

i get this now..

Please enter a possible perfect number: 0
0:NOT PERFECT0:

i got it!!!

i needs to be:
```if (sum != value || sum ==0) {
System.out.printf("%d:NOT PERFECT", value);
}
if (sum == value && sum != 0) {
System.out.printf("%d: ", value);

```

thanks to everyone that helped me out with this program, i appreciate it very much! God night y'all.

good***