Monday, 1 September 2014

Generate first n Prime number

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;
}

No comments:

Post a Comment