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

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…
and we can try to help you understand/learn/get it running

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?

and you not repair your code posting in your topic!

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

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

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+" " );

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.