Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
1,490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 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.            
  16.             int sumprime = 0;
  17.             int sumnoprime = 0;
  18.  
  19.             while (input != "stop")
  20.             {
  21.                 bool isprime = true; // направих променливата ти true.
  22.                 int n = int.Parse(input);
  23.                 if (n < 0)
  24.                 {
  25.                     n = 0;
  26.                     Console.WriteLine("Number is negative.");                  
  27.                    
  28.                 }
  29.  
  30.                 else if (n == 1)
  31.                 {
  32.                     isprime = false;
  33.                 }
  34.                 else
  35.                 {
  36.                     for (int count = 2; count <= n; count++)
  37.                     {
  38.                         if (n % count == 0 && count != n)                  
  39.                          
  40.                         {
  41.                             isprime = false;
  42.                          
  43.                             break;
  44.                         }
  45.                     }
  46.                
  47.                 }
  48.                 if (isprime)
  49.                 {
  50.                     sumprime += n;
  51.                 }
  52.                 else if (isprime == false)
  53.                 {
  54.                     sumnoprime += n;
  55.                 }
  56.                 input = Console.ReadLine();
  57.             }
  58.             if (input == "stop")
  59.             {
  60.                 Console.WriteLine($"Sum of all prime numbers is: {sumprime}");
  61.                 Console.WriteLine($"Sum of all non prime numbers is: {sumnoprime}");
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement