Simple C Program To Produce A List Of Prime Numbers

Started by aruljothi, Sep 28, 2008, 01:06 PM

Previous topic - Next topic

aruljothi

#define CAP 10000
// Change Caps Value To Increase Search Limit.
             

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

int counter; //Keep Count of number of items in storage
int liststorage [10000];  //Can store upto 1000 Prime Numbers

void primelist ()
{
int i,j;
for (i = 3; i < CAP; i++)   // Loops from 3 to UPPERLIMIT
  {
   for (j = 0; j < counter; j++)
    {
      if (!(i%liststorage[j])) break; // Number is Prime
      if (j == counter-1) //Number is Not Prime
        {
          printf ("\n%d",i);
          liststorage [counter] = i;//Add Item To List
          counter++;//Increment Item Counter
          break;
        }
     }
  }
}
             

int main ()

{
  counter = 1;  // Intialize Counter
  liststorage
  • = 2; // Put 2 in the List
      clrscr ();
      printf ("\nPrime Number List : \n\n2");
      primelist ();
      getch ();
      return 0;
    }