Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace PrimeNonPrime
- {
- class PrimeNonPrime
- {
- static void Main(string[]args)
- {
- int primeSum=0;
- int nonPrimeSum=0;
- string input=Console.ReadLine();
- bool nonPrime=false;
- while(input!="stop")
- {
- nonPrime=false;
- int num=int.Parse(input);
- if(num<0)
- {
- Console.WriteLine("Number is negative.");
- }
- else{
- if(num==0 || num==1)
- {
- nonPrimeSum+=num;
- }
- else
- {
- for(int i=2;i<=num/2;i++)
- {
- if(num%i==0)
- {
- nonPrime=true;
- nonPrimeSum+=num;
- break;
- }
- }
- if(nonPrime==false)
- {
- primeSum+=num;
- }
- }
- }
- input=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