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 exr_07_sumPrimeNonPrime
- {
- class Program
- {
- static void Main(string[] args)
- {
- int sumPrime = 0;
- int sumNonPrime = 0;
- while (true)
- {
- // console input
- string txt = Console.ReadLine();
- // check if there is a stop word
- if (txt == "stop") break;
- // check if num is negative
- int num = int.Parse(txt);
- if (num < 0 )
- {
- Console.WriteLine("Number is negative.");
- }
- else
- {
- int i = 2;
- while (num % i != 0) i++;
- if (i == num)
- sumPrime += num;
- else
- sumNonPrime += num;
- }
- }
- // output the result
- Console.WriteLine($"Sum of all prime numbers is: {sumPrime}");
- Console.WriteLine($"Sum of all non prime numbers is: {sumNonPrime}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement