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