# Prime Numbers up to 120 in decreasing order

Page 1 of 1

## 5 Replies - 5973 Views - Last Post: 03 May 2011 - 04:29 AMRate 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=230376&amp;s=a0ca151b4c03d8cf931fb4bbe7e0553f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MoonBeam080680

Reputation: 0
• Posts: 29
• Joined: 05-February 11

# Prime Numbers up to 120 in decreasing order

Posted 02 May 2011 - 09:50 PM

I am trying to find all the prime numbers under 120 and print them out in decreasing order. Here is the code I have now. The error message I am getting is from the line
```StackOfIntegers stack = new StackOfIntegers();
```

The compiler says it cannot find symbol

```public class Prime {

public static void main(String[] args) {

final int LIMIT = 120;

int count = 0;

StackOfIntegers stack = new StackOfIntegers();

// Find prime numbers and keep repeating to 120

for (int number = 2; number < LIMIT; number++)

if (isPrime(number)) {
stack.push(number);

// Increase the count
count++;

}

// Print the prime numbers in decreasing order

System.out.println("The prime numbers less than 120 are \n");

final int NUMBER_PER_LINE = 10;

while (!stack.empty()) {

System.out.print(stack.pop() + " ");

if (stack.getSize() % NUMBER_PER_LINE == 0)

// advance to the new line
System.out.println();
}

}

public static boolean isPrime(int number) {

// Assume the number is prime

boolean isPrime = true;

for (int divisor = 2; divisor <= number / 2; divisor++) {

//If true, the number is not prime

if (number % divisor == 0) {

// Set isPrime to false, if the number is not prime

isPrime = false;

break; // Exit the for loop

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Prime Numbers up to 120 in decreasing order

### #2 sakshamkum

Reputation: 19
• Posts: 232
• Joined: 09-June 09

## Re: Prime Numbers up to 120 in decreasing order

Posted 02 May 2011 - 10:00 PM

Just check whether you have defined class StackOfIntegers

you can do it without Stack as well.
```//in your main() method
for(int i=120;i>=2;i++)
{
if(isPrime(i))
{
System.out.println(i);
}
}

```

This post has been edited by sakshamkum: 02 May 2011 - 10:03 PM

### #3 MoonBeam080680

Reputation: 0
• Posts: 29
• Joined: 05-February 11

## Re: Prime Numbers up to 120 in decreasing order

Posted 02 May 2011 - 10:15 PM

Thank you, but unfortunately I have to do it with StackOfIntegers and I am not sure how to define the class. I thought what I was doing was defining the class. Sorry for being so dumb about this, I am not very good at this.

### #4 pbl

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

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

## Re: Prime Numbers up to 120 in decreasing order

Posted 03 May 2011 - 02:57 AM

Depends how much your teacher wants you to re-invent the wheel.

The easiest and laziest way would be

```public class StackOfIntegers extends Stack<Integer>{
...
}

```

Now may be you are not allowed to take that shortcut and you will need

```
public class StackOfIntegers {

private Stack<Integer> stack = new Stack<Integer>();

}

```

or he wants you to implement yurself the stack in an array
```public class StackOfIntegers {

private final static int SIZE = 100;
int[] array = new int[SIZE];
int index = 0;
}

```

### #5 sakshamkum

Reputation: 19
• Posts: 232
• Joined: 09-June 09

## Re: Prime Numbers up to 120 in decreasing order

Posted 03 May 2011 - 04:26 AM

If you have defined the StackofIntegers then kindly post the code up

This post has been edited by sakshamkum: 03 May 2011 - 04:27 AM

### #6 pbl

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

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

## Re: Prime Numbers up to 120 in decreasing order

Posted 03 May 2011 - 04:29 AM

Ya, sakshamkum is God dammed right. Why speculating on the where to go until you tell us.