Advertisement
sanyakasarova

03. Sum Prime Non Prime

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