Advertisement
clipro

Untitled

Nov 7th, 2018
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 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 exr_07_sumPrimeNonPrime
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int sumPrime = 0;
  14.             int sumNonPrime = 0;
  15.                        
  16.             while (true)
  17.             {
  18.                 // console input
  19.                 string txt = Console.ReadLine();
  20.  
  21.                 // check if there is a stop word
  22.                 if (txt == "stop") break;
  23.  
  24.                 // check if num is negative
  25.                 int num = int.Parse(txt);
  26.                 if (num < 0 )
  27.                 {
  28.                     Console.WriteLine("Number is negative.");
  29.                 }
  30.                 else
  31.                 {
  32.                     int i = 2;
  33.  
  34.                     while (num % i != 0) i++;
  35.  
  36.                     if (i == num)
  37.                         sumPrime += num;
  38.                     else
  39.                         sumNonPrime += num;
  40.                 }
  41.             }
  42.  
  43.             // output the result
  44.             Console.WriteLine($"Sum of all prime numbers is: {sumPrime}");
  45.             Console.WriteLine($"Sum of all non prime numbers is: {sumNonPrime}");
  46.  
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement