Loading...

Print Primes till 'n'

Programme:

#include <stdio.h>
 
int main() {
 
  int n, i = 3, count, c;
 
  printf("Enter the number of prime numbers required : ");
  scanf("%d", &n);
 
  if (n >= 1) {
 
    printf("First %d prime numbers are :\n", n);
    printf("2\n");
 
  }
 
  for (count = 2; count <= n;) {
 
    for (c = 2; c <= i - 1; c++) {
 
      if (i % c == 0)
        break;
 
    }
 
    if (c == i) {
 
      printf("%d\n", i);
      count++;
 
    }
 
    i++;
  }
 
  return 0;
}

Output:

Enter the number of prime numbers required : 6
First 6 prime numbers are :
2
3
5
7
11
13