Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Sum of Prime/NonPrime numbers

JOHNYTHEWINNER Feb 17th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # include <iostream>
  2. using namespace std;
  3. #include <iomanip>
  4. #include <math.h>
  5. #include <cmath>
  6. #include <string>
  7.  
  8. int main()
  9. {
  10.     string number;
  11.     int sumPrime = 0;
  12.     int sumNonPrime = 0;
  13.     int tempnumber;
  14.  
  15.     while (number != "stop") {
  16.         cin >> number;
  17.         if (number == "stop") {
  18.             break;
  19.         }
  20.         tempnumber = stoi(number);
  21.         if (tempnumber < 0) {
  22.             cout << "Number is negative." << endl;
  23.         }
  24.         else {
  25.             bool isPrime = true;
  26.             int i = 2;
  27.             while (i <= tempnumber / 2)
  28.             {
  29.                 if (tempnumber % i == 0) {
  30.                     sumNonPrime += tempnumber;
  31.                     isPrime = false;
  32.                     break;
  33.                 }
  34.                 i++;
  35.             }
  36.             if (isPrime) {
  37.                 sumPrime += tempnumber;
  38.             }
  39.         }
  40.     }
  41.     cout << "Sum of all prime numbers is: " << sumPrime << endl;
  42.     cout << "Sum of all non prime numbers is: " << sumNonPrime << endl;
  43.  
  44.     return 0;
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top