Advertisement
Tusohian

Sieve Algorithm

Nov 23rd, 2017
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int n, i, j, a[1000];
  7.  
  8.     cout<<"Enter The Limit = ";
  9.     cin>>n;
  10.  
  11.     for(i=1; i<=n; i++)
  12.     {
  13.         a[i]=i;
  14.     }
  15.  
  16.     for(i=2; i<=n; i++)
  17.     {
  18.         if(a[i] != -1)
  19.         {
  20.             for(j=2*i; j<=n; j=j+i)
  21.                 a[j]= -1 ;
  22.         }
  23.     }
  24.  
  25.     cout<<"Prime numbers =  ";
  26.     for(i=2; i<=n; i++)
  27.     {
  28.         if(a[i] != -1)
  29.            cout<<" "<< i ;
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement