Friday, November 23, 2012

WRITE A C PROGRAM TO GENERATE 'N' PRIME NUMBERS



/*  WRITE A C PROGRAM TO GENERATE 'N' PRIME NUMBERS. */

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

void main()
{
            int n,m,flag,i,count=0;
            clrscr();
            printf("\n Enter The Value for 'N' : ");
            scanf("%d",&n);
            printf("\n The %d Prime No. are...\n",n);
            for(i=2;count<n;i++)
            {
                        flag=prime(i);
                        if(flag==0)
                        {
                                    printf(" %d \n",i);
                                    count++;
                        }
            }
            getch();
}
/* FUNCTION TO FIND PRIME NO. */
prime(int i)
{
            int j,f;
            f=0;
            for(j=2;j<=i/2;j++)
            if(i%j==0)
            f=1;
            return(f);
}






/*
            =====OUT PUT=====
           
            1. Enter The Value for 'N' : 3

           The 3 Prime No. are...
               2
               3
               5
            -------------------------------
            2. Enter The Value for 'N' : 9

               The 9 Prime No. are...
           2
               3
               5
               7
               11
               13
               17
               19
               23

*/

0 comments:

Post a Comment