Write a C++ program that uses an array determine and display the prime numbers between 2 and n

Hi,i need to write program to print prime number between 1 to n(user input),please give me outline :) I have absolutely no idea :(


Write a C++ program that uses an array of n elements (where n is a user input) to determine
and display the prime numbers between 2 and n using the method described below. Ignore
array elements 0 and 1.

A prime number is any integer greater than one that is divisible only by itself and 1. A list of
prime numbers can be found by the following method.

a) Create a primitive type bool array with all elements initialized to true. Array elements
with prime indices will remain true. All other array elements will eventually be set to
false.

b) Starting with array index 2, determine whether a given element is true. If so, loop
through the remaining of the array and set to false every element whose index is a
multiple of the index for the element with value true.

c) Then continue the process with the next element with value true.
For array index 2, all elements beyond element 2 in the array that have indices which are
multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to false; for array index 3, all elements
beyond element 3 in the array that have indices which are multiples of 3 (indices 6, 9, 12,
15, etc.) will be set to false; and so on.

d) When this process completes, the array elements that are still true indicate that the index
is a prime number. These indices can be displayed.


Multiple sessions of sample screen display when the method is called are given below:
Enter the value of n: 10
Prime numbers: 2, 3, 5, 7
4 primes found.
Enter the value of n: 60
Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53, 59
17 primes found.
Enter the value of n: 100
Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79, 83, 89, 97
25 primes found.
The input which is underlined is the user’s input to a question. (Note: you don’t have to
repeatedly execute your program code using loop for this assignment)
can anyone please help :)
Topic archived. No new replies allowed.