SvetlanPetrova

Sum Prime Non Prime SoftUni

Mar 31st, 2019 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  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.             bool isPrime = true;
  11.             int sumPrime = 0;
  12.             int sumNonPrime = 0;
  13.  
  14.             while (command != "stop")
  15.             {
  16.                 int num = int.Parse(command);
  17.                 if (num < 0)
  18.                 {
  19.                     Console.WriteLine("Number is negative.");
  20.                     continue;
  21.                 }
  22.                 else if (num == 1)
  23.                 {
  24.                     isPrime = false;
  25.                 }
  26.                 else if (num == 0)
  27.                 {
  28.                     isPrime = false;
  29.                 }
  30.                 else
  31.                 {
  32.                     for (int i = 2; i < num; i++)
  33.                     {
  34.                         if (num % i == 0)
  35.                         {
  36.                             isPrime = false;
  37.                             break;
  38.                         }
  39.                     }
  40.                 }
  41.                 if (isPrime)
  42.                 {
  43.                     sumPrime += num;
  44.                 }
  45.                 else if (!isPrime)
  46.                 {
  47.                     sumNonPrime += num;
  48.                 }
  49.                 command = Console.ReadLine();
  50.             }
  51.  
  52.             Console.WriteLine($"Sum of all prime numbers is: {sumPrime}");
  53.             Console.WriteLine($"Sum of all non prime numbers is: {sumNonPrime}");
  54.         }
  55.     }
  56. }
Add Comment
Please, Sign In to add comment