IvetValcheva

Untitled

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