Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp37
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. string command = Console.ReadLine();
  10. int primeSum = 0;
  11. int nonPrimeSum = 0;
  12.  
  13. while (command != "stop")
  14. {
  15. int num = int.Parse(command);
  16. if (num < 0)
  17. {
  18. Console.WriteLine($"Number is negative.");
  19.  
  20. }
  21. else
  22. {
  23.  
  24. bool prime = true;
  25. for (int i = 2; i <= num-1; i++)
  26. {
  27. if (num % i == 0)
  28. {
  29. prime = false;
  30. break;
  31. }
  32. }
  33. if (prime)
  34. {
  35. primeSum += num;
  36. }
  37. else
  38. {
  39. nonPrimeSum += num;
  40. }
  41.  
  42. }
  43. command = Console.ReadLine();
  44.  
  45. }
  46. Console.WriteLine($"Sum of all prime numbers is: {primeSum}");
  47. Console.WriteLine($"Sum of all non prime numbers is: {nonPrimeSum}");
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement