C Program to print first n prime numbers

Simple C program to print first n prime numbers: Prime number is that number which is divisible only by 1 and itself. First prime number is 2 and prime number are 2,3,5,7,11,13,17,19... . This is a simple C program to print first n prime number. If you enter 5 then it will print first 5 prime numbers that are 2,3,5,7,11.
#include<stdio.h>
int main()
{
 int n,i,j,num=3;
 printf("How many you want to print: ");
 scanf("%d",&n);
 printf("Prime numbers are: ");
 printf("%d ",2);
 for(i=1;i<n;i++)
 {
  for(j=2;j<num;j++)
  {
   if(num%j==0)
   {
   i--;
   break;
  }
  }
  if(j==num)
  {
   printf("%d ",num);
  }
  num++;
 }
 return 0;
}



Popular posts from this blog