garryhtreez

6.29

Dec 13th, 2020
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /*6.29
  2. Deitel & Deitel C++ How to Program, 10th ed(Indian subcontinent adaptation)
  3. Visual Studio Community 2019 */
  4.  
  5. #include <iostream>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. bool isPrime(int testNum) {
  10.     if ( testNum == 2 ) return true;
  11.     if (testNum % 2 == 0) return false;
  12.  
  13.     for (int divisor = 2; divisor <= sqrt(testNum); divisor++) {
  14.         if (testNum % divisor == 0) return false;
  15.     }
  16.     return true;
  17. }
  18.  
  19. int main() {
  20.     int count{ 0 };
  21.     for (int i{ 2 }; i <= 10000; i++) {
  22.         if (isPrime(i)) {
  23.             cout << i << " is prime." << endl;
  24.             count++;
  25.         }
  26.     }
  27.     cout << "Total Number of primes from 1 to 10,000: " << count;
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment