This C++ program to implement Sieve of Eratosthenes. The program initializes an integer array with all the elements initialized to 0. Then the algorithm follows where the each non-prime element’s index is marked as 1 inside the nested loops. The prime numbers are those whose value of index is marked as 0.
Here is the source code of the C++ program illustrates how to pass a variable to a function by value. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to implement Sieve of Eratosthenes
*/
#include <iostream>
const int len = 100;
int main()
{
int arr[100] = {0};
for (int i = 2; i < 100; i++)
{
for (int j = i * i; j < 100; j+=i)
{
arr[j - 1] = 1;
}
}
for (int i = 1; i < 100; i++)
{
if (arr[i - 1] == 0)
std::cout << i << "\t";
}
}
$ a.out 1 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
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for Information Technology Internship
- Apply for C++ Internship
- Apply for Computer Science Internship