mitakvd

06. Sum Prime Non Prime

Jul 15th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main () {
  8.  
  9.     string input;
  10.     cin >> input;
  11.  
  12.     int prime = 0;
  13.     int nonPrime = 0;
  14.  
  15.     while(input != "stop") {
  16.         int number = stoi(input);
  17.  
  18.         if(number >= 0) {
  19.             bool isPrime = true;
  20.             for(int i = 2; i <= number / 2; ++i) {
  21.                 if(number % i == 0) {
  22.                     isPrime = false;
  23.                     break;
  24.                 }
  25.             }
  26.             if(isPrime) {
  27.                 prime += number;
  28.             } else {
  29.                 nonPrime += number;
  30.             }
  31.         } else {
  32.             cout << "Number is negative." << endl;
  33.         }
  34.         cin >> input;
  35.     }
  36.  
  37.     cout << "Sum of all prime numbers is: " << prime << endl;
  38.     cout << "Sum of all non prime numbers is: " << nonPrime << endl;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment