C Program to generate N Prime Numbers.

 


#include<stdio.h>

main()

{

int n,np,i,c=0,flag;

clrscr();

printf("\nTotal number of prime numbers needed\n");

scanf("%d",&np);

n=2;

printf("\n Following are the Prime Numbers: \n");

while(1)

{

flag=1;

for(i=2;i<=n/2;i++)

{

if(n%i==0)

{

flag=0;

break;

}

}

if(flag==1)

{

c++;

printf("%d\t",n);

}

if(c==np)

break;

n++;

}

getch();

}



Output:


  1. Total number of prime numbers needed

     4

     Following are the Prime Numbers:

  1.       3       4       5



  1. Total number of prime numbers needed

       10


       Following are the Prime Numbers:

    2       3       4       5       6       7       8       9       10      11




Post a Comment

Previous Post Next Post