SHARE
TWEET

Prime Number Checker

a guest Nov 30th, 2012 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class PrimeNumberChecker
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Title = "Prime Number Checker";
  8.  
  9.         Console.WriteLine("Please enter a positive integer in the range [1, 100] to\n" +
  10.                             "check if it is prime or not:");
  11.  
  12.     Prompt:
  13.         string inputString = Console.ReadLine();
  14.         byte input;
  15.  
  16.         if (byte.TryParse(inputString, out input) && input <= 100 && input >= 1)
  17.         {
  18.             if ((input > 10 && input % 2 != 0 && input % 3 != 0 && input % 5 != 0 && input % 7 != 0) || (input == 2 || input == 3 || input == 5 || input == 7))
  19.             // 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.
  20.             // The only prime numbers less than 10 are 2, 3, 5 and 7.
  21.             {
  22.                 Console.WriteLine("{0} is prime.", input);
  23.                 goto Prompt;
  24.             }
  25.             else if (input == 1)
  26.             {
  27.                 Console.WriteLine("1 is neither prime nor composite.");
  28.                 goto Prompt;
  29.             }
  30.             else
  31.             {
  32.                 Console.WriteLine("{0} is composite.", input);
  33.                 goto Prompt;
  34.             }
  35.         }
  36.         else
  37.         {
  38.             Console.WriteLine("Invalid input. Please enter a positive integer in the\n" +
  39.                                 "range [1, 100]:");
  40.             Console.WriteLine();
  41.             goto Prompt;
  42.         }
  43.     }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top