Advertisement
ralichka

Nested.Loop.Exercises-06.Prime.NonPrime

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