Advertisement
Valantina

SumPrimeNonPrime

Jun 9th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 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.  
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string command = string.Empty;
  15.             int sumPrime = 0;
  16.             int sumNonPrime = 0;
  17.             int commandToInt = 0;
  18.             int ifPrime = 0;
  19.  
  20.  
  21.             while (command != "stop")
  22.  
  23.  
  24.             {
  25.                 command = Console.ReadLine();
  26.                 if (command == "stop")
  27.                 {
  28.                     Console.WriteLine($"Sum of all prime numbers is: {sumPrime}");
  29.                     Console.WriteLine($"Sum of all non prime numbers is: {sumNonPrime}");
  30.                 }
  31.                 else
  32.                 {
  33.  
  34.                     commandToInt = int.Parse(command);
  35.                     if (commandToInt < 0)
  36.                     {
  37.                         Console.WriteLine("Number is negative.");
  38.                     }
  39.  
  40.                     else
  41.                     {
  42.                         for (int i = 1; i <= commandToInt; i++)
  43.                         {
  44.                             if (commandToInt % i == 0)
  45.                             {
  46.                                 ifPrime++;
  47.                             }
  48.  
  49.                         }
  50.                         if (ifPrime == 2)
  51.                         {
  52.                             sumPrime = sumPrime + commandToInt;
  53.                          
  54.                         }
  55.                         else
  56.                         {
  57.                             sumNonPrime = sumNonPrime + commandToInt;
  58.  
  59.                         }
  60.                         ifPrime = 0;
  61.                     }
  62.                 }
  63.  
  64.  
  65.             }
  66.  
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement