Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SumPrimeNonPrime
- {
- class Program
- {
- static void Main(string[] args)
- {
- string command = Console.ReadLine();
- int sumPrime = 0;
- int sumNonPrime = 0;
- while (command != "stop")
- {
- bool isPrime = true;
- int num = int.Parse(command);
- if (num < 0)
- {
- Console.WriteLine("Number is negative.");
- command = Console.ReadLine();
- continue;
- }
- if (num == 1)
- {
- isPrime = false;
- }
- if (num == 0)
- {
- isPrime = false;
- }
- else
- {
- for (int i = 2; i < num; i++)
- {
- if (num % i == 0)
- {
- isPrime = false;
- break;
- }
- }
- }
- if (isPrime)
- {
- sumPrime += num;
- }
- else if (!isPrime)
- {
- sumNonPrime += num;
- }
- command = Console.ReadLine();
- }
- Console.WriteLine($"Sum of all prime numbers is: {sumPrime}");
- Console.WriteLine($"Sum of all non prime numbers is: {sumNonPrime}");
- }
- }
- }
Add Comment
Please, Sign In to add comment