Advertisement
dimipan80

3.8 Check Number Up to 100 Is Prime

Jun 5th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. // Write an expression that checks if given positive integer number n (n ≤ 100) is prime (i.e. it is divisible without remainder only to itself and 1).
  2.  
  3. namespace _08.PrimeNumberCheck
  4. {
  5.     using System;
  6.  
  7.     public class PrimeNumberCheck
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 Console.Write("Enter a whole number, [num <= 100]: ");
  14.                 int num = int.Parse(Console.ReadLine());
  15.                 bool numIsPrime = false;
  16.                 num = Math.Abs(num);
  17.                 if ((num == 2 || num == 3 || num == 5 || num == 7) ||
  18.                     num != 1 && num % 2 != 0 && num % 3 != 0 && num % 5 != 0 && num % 7 != 0)
  19.                 {
  20.                     numIsPrime = true;
  21.                 }
  22.                
  23.                 Console.WriteLine("Given number Is Prime: {0} !", numIsPrime);
  24.             }
  25.         }        
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement