Breaking News

recent posts

Top websites For coding practice include this blog

Coding Practice:-  

                                  1) FIRST PREFERENCE
                                    
                                         hhackerrank.blogspot.in

                                   2) SECOND PREFERENCE




Prime Number Code:-
#include <bits/stdc++.h>
using namespace std;
 
void algo(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
 
    for (int p=2; p*p<=n; p++)
    {
      
        if (prime[p] == true)
        {
            // Update all multiples of p
            for (int i=p*2; i<=n; i += p)
                prime[i] = false;
        }
    }
 
    // Print all prime numbers
    for (int p=2; p<=n; p++)
       if (prime[p])
          cout << p << " ";
}
 
int main()
{
    int n = 30;
    cout << "Following are the prime numbers smaller "
         << " than or equal to " << n << endl;
    algo(n);
    return 0;
}

Output:-
Following are the prime numbers below 30
2 3 5 7 11 13 17 19 23 29
Top websites For coding practice include this blog Top websites For coding practice include this blog Reviewed by best finisher on 23:45 Rating: 5

No comments:

Powered by Blogger.