C Program For First N Prime Numbers


Aim :- Write a C program to print first n prime numbers.

Prime number logic : a number is prime if it is divisible only by one and itself. Remember two is the only even and the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17, ....


C Program Code :



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

void main()
{

      int n,i = 3, p, c;
      clrscr();
      printf("\nEnter The Number : ");
      scanf("%d", &n);

      if(n>=1)
      {
            printf("\nFirst %d Prime Numbers : \n", n);
            printf("2 \n");
      }
      for(p = 2; p <= n; i++)  
      {
            for(c = 2; c < i; c++)
            {
                  if(i%c == 0)
                  break;
            }
            if(c == i)
            {
                  printf("%d\n", i);
                  p++;  
            }
      }
      getch();
}


Output :

Post a Comment

0 Comments