How to write a c program to generate the first 'n' prime numbers in c programming ?
Solution:
/* Generate the first 'n' prime numbers. */
/*Prime number is a number which is exactly divisible by one and itself only*/
#include<stdio.h>
int main()
{
int range,i,j,r;
printf("enter the range \n");
scanf("%d", &range);
for(i=2;i<=range;i++) //Prime numbers start from the int 2
{
int c=0; // set the count value to 0
for(j=1;j<=i;j++) // the loop for calculating the prime number
{
r=(i%j); // r is the variable assigned for checking the prime criterion
if(r==0)
{
c++; // count gets incremented by 1 if the given number is divisible by j parameter
}
}
if(c==2) // the prime number has only 2 factors; hence the count value is set to 2
{
printf("%d \n",i);
}
}
return(0);
}