thegrudge

PrimeNumberCheck

Nov 12th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. class PrimeNumberCheck
  3. {
  4.     static void Main()
  5.     {
  6.         int n = int.Parse(Console.ReadLine());
  7.         int divisorCounter = 0;
  8.         bool isPrime = false;
  9.  
  10.         //Check if the number is 0 or 1. If it's, it's not prime.
  11.         if (n == 0 || n == 1)
  12.         {
  13.             isPrime = false;
  14.         }
  15.         else
  16.         {
  17.             // if it's not 0 or 1, check if the number is equal to any number less than it.
  18.             for (int i = 2; i <= n; i++)
  19.             {
  20.                 if (n % i == 0 && n != i)
  21.                 {
  22.                     divisorCounter++;
  23.                 }
  24.             }
  25.  
  26.             if (divisorCounter == 0)
  27.             {
  28.                 isPrime = true;
  29.             }
  30.             else
  31.             {
  32.                 isPrime = false;
  33.             }
  34.         }
  35.         Console.WriteLine(isPrime);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment