Advertisement
wigllytest

Untitled

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