Thursday , September 21 2017
Home / C / C Program to count prime numbers in given range

C Program to count prime numbers in given range

This is the program to count total number of Prime numbers with in 1 to specified range 

#include<conio.h>
long countprime(long a,long k)
{
long i;
for(i=a;i>=2;i--)
{
if(k%i==0)
return 0;
}
return 1;
}
void main()
{
long i,range;
long count=1;
double x;
printf("Enter the range: ");
scanf("%ld\n",&range);
for(i=3;i<range;i++)
{
x=sqrt(i);
count+=countprime(floor(x),i);
}
printf("\n Total number of prime numbers are %ld",count);
getch();
}

Output

These are the some sample output which you can check using this programs.
1104
210025
31,000168
410,0001,229
5100,0009,592
61,000,00078,498
710,000,000664,579
8100,000,0005,761,455
91,000,000,00050,847,534
tags: Prime Numbers, Program to find total number of prime numbers

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

2 comments

  1. this program gives the output quite close but not the actual wright answer. I am wildly assuming that this is the code for the legendary Riemann Zeta Function.

    Anyway but the actual code will be :-

    #include
    #include
    void main()
    {

    int i,j,k,count=0,l,r;
    clrscr();

    printf(“Enter the lower limit. “);
    scanf(“%d”,&k);
    printf(“Enter the upper limit. “);
    scanf(“%d”,&l);
    printf(“\n”);

    for (i=k ; i<=l ; i++)
    {
    for (j=2 ; j<i ; j++)
    {

    r=i%j;

    if (r==0)
    break;

    }

    printf("\n");

    if (r!=0)
    {
    printf("%d is Prime\n",i);
    count++;
    }
    else

    printf("%d is Composite\n",i);

    }

    printf("\n\n Total no. of Primes is %d",count);
    printf("\n Total no. of Composites is %d",l-k+1-count);

    getch();
    }

    Enter the lower limit. 2
    Enter the upper limit. 10

    the output will be—

    2 is Prime

    3 is Prime

    4 is Composite

    5 is Prime

    6 is Composite

    7 is Prime

    8 is Composite

    9 is Composite

    10 is Composite

    Total no. of Primes is 4
    Total no. of Composites is 5

  2. this program gives the output quite close but not the actual wright answer. I am wildly assuming that this is the code for the legendary Riemann Zeta Function.

    Anyway but the actual code will be :-

    #include
    #include
    void main()
    {

    int i,j,k,count=0,l,r;
    clrscr();

    printf(“Enter the lower limit. “);
    scanf(“%d”,&k);
    printf(“Enter the upper limit. “);
    scanf(“%d”,&l);
    printf(“\n”);

    for (i=k ; i<=l ; i++)
    {
    for (j=2 ; j<i ; j++)
    {

    r=i%j;

    if (r==0)
    break;

    }

    printf("\n");

    if (r!=0)
    {
    printf("%d is Prime\n",i);
    count++;
    }
    else

    printf("%d is Composite\n",i);

    }

    printf("\n\n Total no. of Primes is %d",count);
    printf("\n Total no. of Composites is %d",l-k+1-count);

    getch();
    }

    Enter the lower limit. 2
    Enter the upper limit. 10

    the output will be—

    2 is Prime

    3 is Prime

    4 is Composite

    5 is Prime

    6 is Composite

    7 is Prime

    8 is Composite

    9 is Composite

    10 is Composite

    Total no. of Primes is 4
    Total no. of Composites is 5

Leave a Reply

Your email address will not be published. Required fields are marked *