In: Computer Science
IN C
int count_primes(int start, int end)
{
//TODO: return the count of prime numbers in range [start, end] inclusive.
return 0;
}
#include<stdio.h>
int count_primes(int start,int end)//function to return count of
prime numbers
{
int flag, count=0, i, j;
for(i=start; i<=end; i++)
{
flag = 0;
//verifying whether number is divisible by any other number except
1
for(j=2; j <= i/2; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0 && i>=2)
{
count++;//count the prime numbers
}
}
return count;
}
int main()//program execution starts here
{
int start, end;
printf("Enter starting number in range: ");
scanf("%d", &start);
printf("Enter ending number in range: ");
scanf("%d", &end);
//Prints count of prime numbers in given range[start,end]
printf("\n Number of prime numbers in the given range[start,end] =
%d", count_primes(start,end));
return(0);
}