Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 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. int primeSum = 0;
  15. int nonPrimeSum = 0;
  16.  
  17. while (command != "stop")
  18. {
  19. int num = int.Parse(command);
  20. if (num < 0)
  21. {
  22. Console.WriteLine("Number is negative.");
  23. num++;
  24. break;
  25.  
  26. }
  27.  
  28. bool isPrime = true;
  29. if (num == 1)
  30. {
  31. isPrime = false;
  32. }
  33. else
  34. {
  35. for(int i = num; i >= 2; i--)
  36. {
  37. if(num % i == 0 && i != num)
  38. {
  39. isPrime = false;
  40. break;
  41. }
  42. }
  43. }
  44. if (isPrime)
  45. {
  46. primeSum += num;
  47. }
  48. else
  49. {
  50. nonPrimeSum += num;
  51. }
  52.  
  53.  
  54. command = Console.ReadLine();
  55. }
  56. Console.WriteLine($"Sum of all prime numbers is: {primeSum}");
  57. Console.WriteLine($"Sum of all non prime numbers is: {nonPrimeSum}");
  58.  
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement