prime number in a range code example
Example: prime number in a range
#include <iostream>
#include<math.h>
using namespace std;
int count_prime(int n)
{
bool bo[n+1];
bo[0]=false;
bo[1]=false;
for(int i=2;i<=n;i++)
{
bo[i]=true;
}
int count=0;
for(int i=2;i<=sqrt(n);i++)
{
for(int j=2*i;j<=n;j=j+i)
{
bo[j]=false;
}
}
for(int i=2;i<=n;i++)
{
if(bo[i]==true)
{
count++;
}
}
return count;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int ans=count_prime(n);
cout<<ans<<endl;
}
return 0;
}