Using c Create a program to find all the prime numbers betwe
Using c++
Create a program to find all the prime numbers between 1 and a enterted value . This time, use a classic
method called the \"Sieve of Eratosthenes.\"
Solution
#include <iostream>
using namespace std;
int main()
{
int n=0;
cout<<\"Please Enter the number upto to which you want to find the prime number:\";
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
arr[i]=0; //intializing all the array elements to 0
}
for (int i=2;i<n;i++)
{
for (int j=i*i;j<n;j=i+j)
{
arr[j-1] = 1; //setting non prime numbers to 1
}
}
for (int i=1;i<n;i++)
{
if (arr[i-1]==0) //the array elements whose value is 0 are prime numbers
cout<<i<<\" \"; //printing out the array index which are the prime numbers indeed.
}
}
