# Determine perfect number, error with infinite loop

Page 1 of 1

## 5 Replies - 977 Views - Last Post: 14 April 2013 - 07:40 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=318531&amp;s=7e2c80cd52287ff9b37db39264312fd7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dollymonroe

Reputation: 0
• Posts: 11
• Joined: 14-April 13

# Determine perfect number, error with infinite loop

Posted 14 April 2013 - 06:41 PM

I am having two issues with my program. 1) I cannot figure out how to loop to the next determination when the user enters more than one number. 2) How to print if a number is perfect or not perfect without it going into an infinite loop or printing it the same number of times as there are spots in the array.

```public class csedersten_Perfect
{
/*
*
*
*
*/

public static void main ( String args[] )
{
int getN;
int getP = getPerfect();
int [] perfect = new int[100];
int x = 1;

boolean test = testPerfect(getP, perfect);

while (x <= perfect.length)
{
printFactors(getP, perfect, test);
}
}

/*
*
*
*
*/

public static int getNum()
{
Scanner i = new Scanner( System.in );
int num;                            //
int perfectNum = 1;                 //
int total = 0;                      //

System.out.print( "How many numbers would you like to test? ");
num = i.nextInt();

boolean validateN = validateNum(num, perfectNum);

while (!validateN)
{
System.out.print( "How many numbers would you like to test? ");
num = i.nextInt();

validateN = validateNum(num, perfectNum);
}
return num;
}

/*
*
*
*
*/

public static boolean validateNum( int num, int perfectNum )
{
if (( num <= 0 ) || ( perfectNum <= 0 ))
{
System.out.println( " Non-positive numbers are not allowed." );
}

else
{
return true;
}
return false;
}

/*
*
*
*
*/

public static int getPerfect()
{
Scanner i = new Scanner ( System.in );

int perfectNum = -1;
int num = getNum();

System.out.print( "Please enter a possible perfect number: " );
perfectNum = i.nextInt();

boolean validateN = validateNum(perfectNum, num);

while (!validateN)
{
System.out.print( "Please enter a possible perfect number: " );
perfectNum = i.nextInt();

validateN = validateNum(perfectNum, num);
}
return perfectNum;
}

/*
*
*
*
*/

public static boolean testPerfect( int perfectNum, int[] perfect )
{
int max = perfectNum;
int a = 0;
int sum = 0;

for (int x = 1; x <= max; x++)
{
perfect[a++] = x;
perfectNum /= x;
}

perfect[a] = perfectNum;

for (int x = 0; x < perfect.length; x++)
{
sum += perfect[x];
}

if (sum == perfectNum)
{
int[] p = perfect;

return true;
}

else
{
return false;
}
}

/*
*
*
*
*/

public static void printFactors( int perfectNum, int[] perfect, boolean test)
{

if (test == true)
{
System.out.printf( "%d:%10d", perfectNum, perfect);
}

else
{
System.out.printf( "%d:NOT PERFECT", perfectNum );
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Determine perfect number, error with infinite loop

### #2 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 06:49 PM

Your while loop is riding on the integer x being less than 100, it is set to 1. There is nothing in your code that will make x ever be more than 100, hence, you have an infinite loop.

### #3 dollymonroe

Reputation: 0
• Posts: 11
• Joined: 14-April 13

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:06 PM

I understand what you're saying, but when I enter a number to determine if it is perfect or not perfect, the program doesn't stop printing the same thing over and over. I have to type ctrl c to escape it. I was wondering how to fix that.

### #4 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:19 PM

You obviously didn't understand what I said, because addressing the issue I pointed out would correct your infinite loop that's printing everything out.

```while (x <= perfect.length)
```

The int 'x' is 1.
The length of your array 'perfect' is [100]

You never change x in your code. x is always <= perfect.length.

I think you are trying to track how many inputs there are, an array length is not the way to do this since arrays have a set length at birth. What you will want to do is have a variable to record the number of inputs, and use that value in a for loop.

```//class variable
static int numberOfInputs = 0;

System.out.print( "Please enter a possible perfect number: " );
perfectNum = i.nextInt();
numberOfInputs++;

for(int i = 0; i < numberOfInputs; i++) {
print
}
```

### #5 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:28 PM

This does not really make sense

```        int max = perfectNum;
int a = 0;
int sum = 0;
for (int x = 1; x <= max; x++)
{
perfect[a++] = x;
perfectNum /= x;
}

```

so you store
perfect[0] = 1
perfect[1] = 2
perfect[2] = 3
.... up to
perfect[max]

at the same time assuming perfectNum == 10 which mean max == 10
you will
perfectNum /= 1
perfectNum /= 2
...
perfectNum /= 10

sure that perfectNum will be == 0 at the end of the loop

### #6 dollymonroe

Reputation: 0
• Posts: 11
• Joined: 14-April 13

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:40 PM

Flukeshot, I would use that solution if I could, but I am not allowed to use global variables for this assignment.