12,503,947 members (56,869 online)
Rate this:
See more:
I can write the code for "Sieve of Eratosthenes" to generate all the prime number up to n. But I want to write a program that will generate and index all the prime number (up to n) into an array, than check whether a number is prime or not. Can anyone please help me to do it?

For Example: if n = 10, than the prime number is 2 3 5 7. Index them into an array a[ ] = {2, 3, 4, 7}. Than write a program and press a number as input from keyboard, than check the number is prime or not. I know that there is a simple way to check whether a number is prime or not but I want to do it by the above way. How can I do that???
Posted 9-Nov-12 1:44am
Updated 9-Nov-12 1:46am
v3

Rate this:

## Solution 1

Since you mark all numbers in your original code as prime or not, you should already have a lookup table which you can use.

Alternatively you could create an array or use a simple `vector`[^] or `list`[^] to store them. Then when the user enters a value you can look it up in the container and report accordingly.

Sieve of Eratosthenes in C#[^] is a sample article that should help.

Top Experts
Last 24hrsThis month
 OriginalGriff 440 Maciej Los 268 ppolymorphe 175 F-ES Sitecore 170 Karthik Bangalore 122
 OriginalGriff 7,133 ppolymorphe 3,874 Maciej Los 3,541 David_Wimbley 2,872 Karthik Bangalore 2,742