Advertisement
Guest User

06. Sum Prime Non Prime

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