SHARE
TWEET

Prime Numbers

lmarkov Nov 26th, 2012 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class PrimeNumbers
  4. {
  5.     static void Main()
  6.     {
  7.         uint number;
  8.         bool checkResult;
  9.         int divisionsCheck = 0;
  10.  
  11.         Console.WriteLine("Check if given positive number is prime. Enter your number n(n<=100): ");
  12.  
  13.         if (uint.TryParse(Console.ReadLine(), out number) && number > 1 && number <= 100)
  14.         {
  15.             for (int i = 2; i < number; i++)
  16.             {
  17.                 checkResult = number % i == 0;
  18.                 if (checkResult)
  19.                 {
  20.                     divisionsCheck = divisionsCheck + 1;
  21.                 }
  22.             }
  23.             if (divisionsCheck == 0)
  24.             {
  25.                 Console.WriteLine("The number {0} is a prime number.\n", number);
  26.             }
  27.             else
  28.             {
  29.                 Console.WriteLine("The number {0} isn't a prime number.\n", number);
  30.             }
  31.             Main();
  32.         }
  33.         else
  34.         {
  35.             if(number == 0 || number == 1)
  36.             {
  37.                 Console.WriteLine("0 and 1 aren't prime numbers!");
  38.             }
  39.             Console.WriteLine("Invalid input! Please enter a number which is greater then 1 and smaller or equal to 100!\n");
  40.             Main();  
  41.         }
  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