Advertisement
Guest User

check for prime

a guest
Mar 14th, 2014
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class PrimeNumCheck
  8. {
  9.     static void Main()
  10.     {
  11.         uint intToCheck;
  12.          int count = 0;
  13.         Console.WriteLine("Provide a positive integer <= 100 to check:");
  14.         while (!(uint.TryParse(Console.ReadLine(), out intToCheck) && (intToCheck <= 100)))
  15.         {
  16.             Console.WriteLine("Please, enter valid integer.");
  17.         }
  18.         if (intToCheck != 1)
  19.         {
  20.             for (int i = 2; i <= Math.Sqrt(intToCheck); i++)
  21.             {
  22.                 count = count + (((intToCheck % i) == 0) ? 1 : 0);
  23.             }        
  24.         }
  25.         else
  26.         {
  27.             count = 2;
  28.         }
  29.  
  30.         Console.WriteLine((count > 0) ? "Is Not Prime" : "Is Prime");
  31.         Console.WriteLine("\nPress any key to exit");
  32.         Console.ReadKey();    
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement