# C Program to Generate First N Prime Numbers Where N is Given by User

A Prime Number is a positive integer greater than 1 which is divisible by 1 and itself. In other words, Prime number is a whole number greater than whose factors are 1 and itself. Examples of first few prime numbers are 2, 3, 5, 7, 11, 13..

In this C program, we are going to generate first N such prime numbers, where N is given by user i.e. if user wants to generate 10 prime numbers, we will generate first 10 prime numbers and similarly if user wants to generate first 100 prime numbers, we will generate first 100 prime numbers and so on. Keep in mind that first prime number is 2 and which is only even prime number.

## Program

``````
#include<stdio.h>
#include<conio.h>

int main()
{
int n, count=1, flag, i=2, j;
clrscr();
printf("Enter how many prime numbers? \n");
scanf("%d", &n);

/* Generating prime numbers */
while(count <= n)
{
flag = 0;
for(j=2; j <= i/2; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\t",i);
count++;
}
i++;
}
getch();
return(0);
}
``````

Output of the above program :

```Run 1:
-----------
Enter how many prime numbers?
10 ↲
2   3   5   7  11   13   17   19   23   29

Run 2:
-----------
Enter how many prime numbers?
50 ↲
2    3     5    7    11   13   17   19   23   29
31   37    41   43   47   53   59   61   67   71
73   79    83   89   97   101  103  107  109  113
127  131   137  139  149  151  157  163  167  173
179  181   191  193  197  199  211  223  227  229

Note: ↲ indicates enter is pressed.
```