Advertisement
Spocoman

03. Sum Prime Non Prime

Sep 12th, 2023
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8.     int prime = 0, nonPrime = 0;
  9.  
  10.     string command;
  11.     cin >> command;
  12.  
  13.     while (command != "stop") {
  14.  
  15.         int number = stoi(command);
  16.  
  17.         if (number >= 0) {
  18.  
  19.             if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0) {
  20.                 if (number == 2 || number == 3 || number == 5 || number == 7) {
  21.                     prime += number;
  22.                 }
  23.                 else {
  24.                     nonPrime += number;
  25.                 }
  26.             }
  27.             else {
  28.                 prime += number;
  29.             }
  30.         }
  31.         else {
  32.             cout << "Number is negative.\n";
  33.         }
  34.         cin >> command;
  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
Advertisement