Advertisement
angelneychev

07. Sum Prime Non Prime

Oct 22nd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp14
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string comand = Console.ReadLine();
  10.             int sumPrime = 0;
  11.             int sumNotPrime = 0;
  12.             int sumNegativ = 0;
  13.            
  14.  
  15.             while (comand != "stop")
  16.             {
  17.                 int currentNum = int.Parse(comand);
  18.                 int saveNumber = currentNum;
  19.                 int b = 0;
  20.                 for (int i = 2; i < currentNum; i++)
  21.                 {
  22.                     if (currentNum % i == 0)
  23.                     {
  24.                         b = 1;
  25.                         break;
  26.                     }
  27.                 }
  28.                 if (saveNumber < 0)
  29.                 {
  30.                     sumNegativ += saveNumber;
  31.                 }
  32.                 else if (b == 0)
  33.                 {
  34.                     sumPrime += saveNumber;
  35.                 }
  36.                 else
  37.                 {
  38.                     sumNotPrime += saveNumber;
  39.                 }
  40.                 comand = Console.ReadLine();
  41.                 if (comand == "stop")
  42.                 {
  43.                     break;
  44.                 }
  45.             }
  46.             if (sumNegativ < 0)
  47.             {
  48.                 Console.WriteLine("Number is negative.");
  49.             }
  50.             Console.WriteLine($"Sum of all prime numbers is: {sumPrime}");
  51.             Console.WriteLine($"Sum of all non prime numbers is: {sumNotPrime}");
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement