Advertisement
BorisSimeonov

Prime Number Check

Nov 10th, 2014
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         try
  8.         {
  9.             int num = int.Parse(Console.ReadLine()); ;
  10.             int counter = (int)Math.Sqrt(num);
  11.             bool isPrime = true;
  12.             if (num > 1)
  13.             {
  14.                 for (int cnt = 2; cnt <= counter; cnt++)
  15.                 {
  16.                     if (num % cnt == 0)
  17.                     {
  18.                         isPrime = false;
  19.                         break;
  20.                     }
  21.                 }
  22.             }
  23.             else
  24.             {
  25.                 isPrime = false;
  26.             }
  27.             Console.WriteLine(isPrime ? "The number {0} is prime number." :
  28.                 "The number {0} is composite number.", num);
  29.         }
  30.         catch
  31.         {
  32.             Console.WriteLine("Invalid Input.");
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement