Dianov

Nested Loops - Exercise (03. Sum Prime Non Prime)

Mar 4th, 2021
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 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 primeSum = 0;
  16.             int nonPrimeSum = 0;
  17.             bool isTrue = true;
  18.  
  19.             while (isTrue)
  20.             {
  21.                 command = Console.ReadLine();
  22.                 int number = 0;
  23.                 if (command != "stop")
  24.                 {
  25.                     number = int.Parse(command);
  26.                 }
  27.                 else
  28.                 {
  29.                     isTrue = false;
  30.                     break;
  31.                 }
  32.  
  33.                 if (number < 0)
  34.                 {
  35.                     Console.WriteLine("Number is negative.");
  36.                 }
  37.                 else if ((number % 2 == 0 && number != 2) || (number % 3 == 0 && number != 3))
  38.                 {
  39.                     nonPrimeSum += number;
  40.                 }
  41.                 else
  42.                 {
  43.                     primeSum += number;
  44.                 }
  45.             }
  46.             Console.WriteLine($"Sum of all prime numbers is: {primeSum}");
  47.             Console.WriteLine($"Sum of all non prime numbers is: {nonPrimeSum}");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment