Lesson 4
Repeated Practice Makes Perfect
Chapter 6: More applied problems
Generating a list of prime numbers
Now, let’s extend the previous program.
Instead of just checking whether one number is prime, I now want you to write a program to list out all prime numbers up to and including N (where N is an integer entered by the user).
So, if the user enters 20
, your program should output:
Please enter an integer: 20
2
3
5
7
11
13
17
19
How would you do this? Again, think about your solution carefully. You might want to draw some diagrams with example numbers to get an idea of how to solve this.
Hint: You may need a nested while loop.
Hint: This can be just a small modification of your previous program.