Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SumPrimeNonPrime
- {
- class SumPrimeNonPrime
- {
- static void Main()
- {
- string input = Console.ReadLine();
- int N = int.Parse(input);
- int nativeSum = 0;
- int nonNativeSum = 0;
- while (input != "stop")
- {
- bool IsPrime(int N)
- {
- if (N <= 1) return false;
- if (N == 2) return true;
- if (N % 2 == 0) return false;
- var boundary = (int)Math.Floor(Math.Sqrt(N));
- for (int i = 3; i <= boundary; i += 2)
- if (N % i == 0)
- return false;
- return true;
- }
- if (IsPrime(N))
- {
- nativeSum += N;
- }
- else
- {
- nonNativeSum += N;
- }
- input = Console.ReadLine();
- if (input == "stop")
- {
- break;
- }
- else
- {
- N = int.Parse(input);
- }
- if (N < 0)
- {
- Console.WriteLine("Number is negative.");
- input = Console.ReadLine();
- N = int.Parse(input);
- }
- }
- Console.WriteLine($"Sum of all prime numbers is: {nativeSum}");
- Console.WriteLine($"Sum of all non prime numbers is: {nonNativeSum}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement