SvetlanPetrova

Sum Prime Non Prime SoftUni

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