Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C prime
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 2:44am
Edited 9-Nov-12 2:46am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sascha Lefévre 365
1 Sergey Alexandrovich Kryukov 325
2 Maciej Los 240
3 Abhinav S 184
4 Peter Leow 120
0 Sergey Alexandrovich Kryukov 6,773
1 OriginalGriff 6,311
2 Maciej Los 2,692
3 Peter Leow 2,654
4 Abhinav S 2,562

Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100