C++ : count total numbers of prime number between 1 to given number ‘n’


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int m,prime=0;

  cout<<"Enter a number to count total numbers of prime number between 1 to entered number : ";
cin>>m;

for(int j=2;j<=m;j++)
{
int flag=1;
for(int i=2;i<=j/2;i++)
{
if(j%i==0)
{
flag=0;
break;
}
}
if(flag)
   prime++;

}

cout<<"\nTotal number of prime number bewteen 1 to "<<m<<" is "<<prime;

getch();
return(0);
}

No comments:

Post a Comment