Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a Boolean method IsPrime(n) that check whether a given integer number n is prime.
- namespace _02.PrimeChecker
- {
- using System;
- public class PrimeChecker
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.Write("Enter a Integer number: ");
- long num = long.Parse(Console.ReadLine());
- num = Math.Abs(num);
- bool isPrimeNum = CheckTheIntegerNumberIsPrimeOrNot(num);
- Console.WriteLine("That number Is Prime: {0} !", isPrimeNum);
- }
- }
- private static bool CheckTheIntegerNumberIsPrimeOrNot(long num)
- {
- checked
- {
- if (num < 2)
- {
- return false;
- }
- else
- {
- bool isPrime = true;
- long maxDivider = (long)Math.Sqrt(num);
- for (long i = 2; i <= maxDivider; i++)
- {
- if (num % i == 0)
- {
- isPrime = false;
- break;
- }
- }
- return isPrime;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement