alexbancheva

Sum_Prime_Non_Prime__Nested_Loops_Exercise

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