Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 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. bool prime = true;
  13. int sumPrime = 0;
  14. int sumNonPrime =0;
  15. int number;
  16.  
  17. while (input != "stop" && input != "STOP"){
  18. int number = stoi (input);
  19.  
  20. if (number < 0){
  21.     cout << "Number is negative." << endl;
  22.     cin >> input;
  23.     continue;
  24. }
  25.       for(int i = 2; i <= number /2; ++i)
  26.   {
  27.           if(number % i == 0)
  28.           {
  29.             prime = false;
  30.             break;
  31.           }
  32.     }
  33.  
  34.     if (prime){
  35.         sumPrime += number;
  36.     } else {
  37.         sumNonPrime += number;
  38.         prime = true;
  39.     }
  40.  
  41.     cin >> input;
  42. }
  43.  
  44.         cout << "Sum of all prime numbers is: " << sumPrime << endl;
  45.  
  46.         cout << "Sum of all non prime numbers is: " << sumNonPrime << endl;
  47.  
  48.  
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement