Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class WhetherIsPrime
- {
- static void Main()
- {
- Console.WriteLine("Please enter positive integer <=100:");
- int positiveInt = int.Parse(Console.ReadLine());
- if (positiveInt > 100 | positiveInt < 0) //Checks whether input conditions are met.
- {
- Console.WriteLine("Invalid number. Please enter positive integer <=100:");
- }
- else
- {
- int sumDividers = 0; //This is the counter of dividers. If it remains zero at the end the integer is prime.
- for (int i = 2; i <= Math.Sqrt(positiveInt); i++)
- {
- if (positiveInt % i == 0)
- {
- sumDividers = sumDividers + 1;
- }
- }
- Console.WriteLine(sumDividers == 0 ? "The number is prime." : "The number is not prime.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement