Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,923 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,727 people online right now. Registration is fast and FREE... Join Now!





Finding Prime Numbers Using The Sieve Of Eratosthenes

This program uses a simple method developed by Eratosthenes(Greek mathematician). Basically u select a number an strike of all its multiplies. then u go to the next number n so on till u reach the end. What ure left with r prime numbers.

Submitted By: Louisda16th
Actions:
Rating:
Views: 10,376

Language: C

Last Modified: August 12, 2006
Instructions: compile, build n run

Snippet


  1. /* Finding Prime Numbers Using The Sieve Of Eratosthenes*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.      int num[100], i, j;
  6.      
  7.      for(i=0;i<=99;i++)     /*Fill num with the first 100 numbers*/
  8.           num[i]=i+1;
  9.      
  10.      for(i=1;i<=99;i++)
  11.      {
  12.           if(num[i]!=0)
  13.           {
  14.                for(j=(i+1);j<=99;j++)
  15.                {
  16.                     if(num[j]!=0)
  17.                     {
  18.                          if((num[j]%num[i])==0)     /*check if num[j]*/
  19.                          num[j]=0;                    /*is a multiple of num[i]*/
  20.                                                        /*if it is a multiple then set it to 0*/
  21.                     }
  22.                }
  23.           }
  24.      }
  25.  
  26.      for(i=0;i<=99;i++)
  27.      {
  28.           if(num[i]!=0)                    /*Print all non zero numbers (prime numbers)*/
  29.                printf("\n%d",num[i]);
  30.      }
  31. }
  32.  
  33.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month