Advertisement
makispaiktis

Prime numbers and ratio

Feb 24th, 2019 (edited)
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int counter = 0;
  9.     int n,i,j;
  10.     printf("Enter n value: ");
  11.     scanf("%d", &n);
  12.     int prime[n+1];
  13.  
  14.     for(i = 1; i <= n; i++)
  15.     {
  16.         prime[i] = i;
  17.     }
  18.  
  19.     for(i = 2; i*i <= n; i++)
  20.     {
  21.         if(prime[i] != 0)
  22.         {
  23.  
  24.             for(j = 2*i; j <=n ; j += i)
  25.                 prime[j] = 0;
  26.         }
  27.     }
  28.     printf("Prime numbers are: \n");
  29.     for(i=2; i <= n; i++)
  30.     {
  31.         if(prime[i] != 0)
  32.         {
  33.             printf("%d ",i);
  34.             counter++;
  35.         }
  36.     }
  37.  
  38.     cout << endl << endl <<"Counter: " << counter << endl;
  39.     cout << "Ratio: " << (double)n/(double)counter;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement