# Print all prime numbers from 1 to n in reverse order

**URL:** https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575
**Category:** Coding Questions
**Tags:** homework
**Created:** [May 25, 2019, 1:44am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575 "2019-05-25T01:44:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anonomous](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anonomous](https://discourse.processing.org/u/anonomous)
#### Post date: [May 25, 2019, 1:44am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/1 "2019-05-25T01:44:29Z")

</div>

Hi! I just started learning Processing 3 weeks ago for a class, so I’ve only learned how to use strings/chars, if/else decision structures, and loops so far. I’m stumped on the very last exercise. Here is the assignment, and I’ve included my attempt at writing this program. Please help!!

* * *

Task 5.2 Numerical Analysis Using Nested Loops (13 pts)  
Consider the following program:

```auto
void setup()
{
//Read a positive integer from the user
//Your code starts here }

```

Complete this program such that it calculates all prime numbers between 1 and the value that is assigned to variable num and outputs them on the screen. A prime number is a positive integer that has no other factors other than itself and 1. You should use a nested loop, i.e., write code where a for-loop runs within another for-loop. DO NOT USE CLASSES/OBJECTS, METHODS, OR APPLETS; simply use a nested loop.

The program should print prime numbers in reverse order, i.e., starting with the largest one.

Example: When num = 10, the program should print: 7 5 3 2 , which are the prime numbers between 1 and 10.

Example: When num = 19, the program should print 19 17 13 11 7 5 3 , which are the prime numbers between 1 and 19.

HINT: If you are having problems with this task, start by writing a for loop to check if a number is prime or not. Once this is complete, nest this loop within another loop to check all numbers in the range given by the user.

* * *

This is the code that I’ve written in attempting this problem. Please modify it and make any appropriate changes. The idea behind my code is that since the numbers have to be printed in reverse order, I’ve initialized i in the outer loop to equal num and then I have a decrementing expression so that it can detect, store, and then print all the prime numbers between 1 and num in reverse order. Note: I am very new to processing (I only started learning it 3 weeks ago, so please don’t go beyond the scope of what I’ve learnt (which is strings/chars, if/else decision structures, and loops).

```auto
void setup(){
  import javax.swing.JOptionPane;
int num; 
num=parseInt(JOptionPane.showInputDialog("Enter a positive, real integer"));
int i;
int j;

for (i = num; i > 1; i--){
  for (j = 2; j < i; j++){
    if (i%j == 0)
    {
      break;
    }
  }
  if (i==j)
  {
    print(num);
  }
}
println();
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 25, 2019, 2:05am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/2 "2019-05-25T02:05:52Z")

</div>

-a- if you post code here please use the

```auto
</> code tag

```

```  
type or paste code here  
```

-b- we are not allowed to do your homework

-c- ask a specific question for a command…  
and we can try to help you understand/learn/get it running

---

<div class="post-metadata">

### Author: ![anonomous](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anonomous](https://discourse.processing.org/u/anonomous)
#### Post date: [May 25, 2019, 3:40am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/3 "2019-05-25T03:40:10Z")

</div>

Because I’m so new to Processing, I have no idea what’s wrong with the code that I’ve written. Please at least help me understand what the issue is with it. When I run the code, it just prints the user’s input 4 times, as opposed to printing all prime numbers between 1 and that number in reverse order.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 25, 2019, 3:44am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/4 "2019-05-25T03:44:45Z")

</div>

well,

- you set num to user input,

- you print num

- you see num

what else you expect?

and you not repair your code posting in your topic!

---

<div class="post-metadata">

### Author: ![anonomous](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anonomous](https://discourse.processing.org/u/anonomous)
#### Post date: [May 25, 2019, 3:46am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/5 "2019-05-25T03:46:32Z")

</div>

```auto
void setup(){
  import javax.swing.JOptionPane;
  int num; 
  num=parseInt(JOptionPane.showInputDialog(“Enter a positive, real integer”));
  int i;
  int j;

  for (i = num; i > 1; i–){
    for (j = 2; j < i; j++){
      if (i%j == 0){
        break;
      }
    }
    if (i==j){
      print(num);
    }
  }
  println();
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 25, 2019, 3:48am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/6 "2019-05-25T03:48:04Z")

</div>

so instead repair your code in the first post you  
make a new post with another wrong code posting,  
now you have to repair/edit BOTH

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 25, 2019, 9:24am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/7 "2019-05-25T09:24:50Z")

</div>

> **[r/processing - Syntax Error](https://www.reddit.com/r/processing/comments/bsqzr3/syntax_error/)**
>
> 1 vote and 16 comments so far on Reddit

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 25, 2019, 11:08am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/8 "2019-05-25T11:08:41Z")

</div>

> [@anonomous](#):
>
> if (i==j)  
> {  
> print(num);  
> }

please analyze what you are printing here and what you want to print instead.

**Remark**

Also I recommend some space signs after each number:  
print(num+" " );

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 27, 2019, 5:29am UTC](https://discourse.processing.org/t/print-all-prime-numbers-from-1-to-n-in-reverse-order/11575/9 "2019-05-27T05:29:41Z")

</div>

> [@anonomous](#):
>
> The idea behind my code is that since the numbers have to be printed in reverse order, I’ve initialized i in the outer loop to equal num and then I have a decrementing expression so that it can detect, store, and then print all the prime numbers between 1 and num in reverse order.

This is a good strategy. Now that you have done that (counting down, from 10 to 1) – how will you check if each number is prime or not? Imagine you were giving instructions to a person.

While editing a forum post, you will see a little button \</\> in the toolbar. Highlight your code and press that button. It will add little ``` around it, making it readable.
