1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include<stdio.h>
#include<string.h>
#include<math.h>
bool isprime(int);
int countprime(int);
bool isprime(int n)
{
int i,j,k,f=0;
int d=sqrt(n);
for(i=2;i<=d;i++)
{
if(n%i==0)
{
f=1;
i=i+d;
}
}
if(f==1)
return false;
else
return true;
}
int countprime(int n)
{
int i,j,k,count=0;
for(i=2;i<=n;i++)
{
int f=0;
int d=sqrt(i);
for(j=2;j<=d;j++)
{
if(i%j==0)
{
f=1;
j=j+n;
}
}
if(f==0)
count++;
}
return count;
}
int main()
{
int i,j,n;
bool k;
printf("Enter an integer :\n");
scanf("%d",&n);
while(n>=0)
{
if(n>1)
{
k=isprime(n);
if(k)
printf("%d is prime ? True\n",n);
else
printf("%d is prime ? False\n",n);
j=countprime(n);
printf("No of primes <= %d : %d\n",n,j);
}
else if(n==0)
{
printf("%d is prime ? False\n",n);
printf("No of primes <= %d : %d\n",n,0);
}
else if(n==1)
{
printf("%d is prime ? False\n",n);
printf("No of primes <= %d : %d\n",n,0);
}
printf("Enter another integer :\n");
scanf("%d",&n);
}
return 0;
}
|