Advertisement
Soulfood

SumPrimeNonPrime 2

Oct 20th, 2019
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SumPrimeNonPrime
  4. {
  5.     class SumPrimeNonPrime
  6.     {
  7.         static void Main()
  8.         {
  9.  
  10.             string input = Console.ReadLine();
  11.             int N = int.Parse(input);
  12.             int nativeSum = 0;
  13.             int nonNativeSum = 0;
  14.  
  15.             while (input != "stop")
  16.             {
  17.                 bool IsPrime(int N)
  18.                 {
  19.                     if (N <= 1) return false;
  20.                     if (N == 2) return true;
  21.                     if (N % 2 == 0) return false;
  22.  
  23.                     var boundary = (int)Math.Floor(Math.Sqrt(N));
  24.  
  25.                     for (int i = 3; i <= boundary; i += 2)
  26.                         if (N % i == 0)
  27.                             return false;
  28.  
  29.                     return true;
  30.                 }
  31.  
  32.                 if (IsPrime(N))
  33.                 {
  34.                     nativeSum += N;
  35.                 }
  36.                 else
  37.                 {
  38.                     nonNativeSum += N;
  39.                 }
  40.  
  41.                 input = Console.ReadLine();
  42.                 if (input == "stop")
  43.                 {
  44.                     break;
  45.                 }
  46.                 else
  47.                 {
  48.                     N = int.Parse(input);
  49.                 }
  50.                 if (N < 0)
  51.                 {
  52.                     Console.WriteLine("Number is negative.");
  53.                     input = Console.ReadLine();
  54.                     N = int.Parse(input);
  55.                 }
  56.             }
  57.             Console.WriteLine($"Sum of all prime numbers is: {nativeSum}");
  58.             Console.WriteLine($"Sum of all non prime numbers is: {nonNativeSum}");
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement