Advertisement
Guest User

Untitled

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