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

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:

``````void setup()
{
//Read a positive integer from the user
``````

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).

``````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();
}
``````

-a- if you post code here please use the

``````</> code tag
``````

```
type or paste code here
```

-b- we are not allowed to do your homework

-c- ask a specific question for a command…

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.

well,

• you set num to user input,

• you print num

• you see num

what else you expect?

``````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();
}
``````