Tuesday, August 7, 2012

Generate first n Prime number

Q. Write a C program to generate first n prime number C program.

Ans.

/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,num,t,div,count;
 printf("How many prime number you want to print: ");
 scanf("%d", &n);
 printf("\n%d\t",2); /*2 is first prime number*/
 count=1;
 num=3;
 while(count<n)
 {
  t=sqrt(num);
  div=2;
  while(div<=t)
  {
    if(num%div==0)
       break;
    div++;
  }
  if(div>t)
  {
     printf("%d\t",num);
     count++;
  }
  num=num+2;
 }
 getch();
 return 0;
}

/************Output************/


Output of generate first n prime number C program
Screen shot for generate first n prime number
C program

Related Programs:

  1. Print Prime Number 1 to 100 C program
  2. Search for Prime Number C program
  3. Flowchart for search prime number

No comments:

Post a Comment