Advertisement
Guest User

Untitled

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