Advertisement
Krissy

CSharp_H3_PrimeNumber

Nov 13th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2.  
  3. class WhetherIsPrime
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Please enter positive integer <=100:");
  8.         int positiveInt = int.Parse(Console.ReadLine());
  9.         if (positiveInt > 100 | positiveInt < 0) //Checks whether input conditions are met.
  10.         {
  11.             Console.WriteLine("Invalid number. Please enter positive integer <=100:");
  12.  
  13.         }
  14.         else
  15.         {
  16.  
  17.             int sumDividers = 0; //This is the counter of dividers. If it remains zero at the end the integer is prime.
  18.             for (int i = 2; i <= Math.Sqrt(positiveInt); i++)
  19.             {
  20.                 if (positiveInt % i == 0)
  21.                 {
  22.                     sumDividers = sumDividers + 1;
  23.                 }
  24.             }
  25.             Console.WriteLine(sumDividers == 0 ? "The number is prime." : "The number is not prime.");
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement