Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class PrimeNumberChecker
- {
- static void Main()
- {
- Console.Title = "Prime Number Checker";
- Console.WriteLine("Please enter a positive integer in the range [1, 100] to\n" +
- "check if it is prime or not:");
- Prompt:
- string inputString = Console.ReadLine();
- byte input;
- if (byte.TryParse(inputString, out input) && input <= 100 && input >= 1)
- {
- if ((input > 10 && input % 2 != 0 && input % 3 != 0 && input % 5 != 0 && input % 7 != 0) || (input == 2 || input == 3 || input == 5 || input == 7))
- // If the input is greater than 10 and not divisible by any of the prime numbers in the range [1, sqrt(100)], i.e. {2, 3, 5, 7} then it is prime.
- // The only prime numbers less than 10 are 2, 3, 5 and 7.
- {
- Console.WriteLine("{0} is prime.", input);
- goto Prompt;
- }
- else if (input == 1)
- {
- Console.WriteLine("1 is neither prime nor composite.");
- goto Prompt;
- }
- else
- {
- Console.WriteLine("{0} is composite.", input);
- goto Prompt;
- }
- }
- else
- {
- Console.WriteLine("Invalid input. Please enter a positive integer in the\n" +
- "range [1, 100]:");
- Console.WriteLine();
- goto Prompt;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement