Prime number check in C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace PrimeNumberCheck
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int number, i;
  13.             Console.WriteLine(“Enter a Number to Check”);
  14.             number = Convert.ToInt32(Console.ReadLine());
  15.             for (i = 2; i < number; i++)
  16.             {
  17.                 if (number%2==0)
  18.                 {
  19.                     Console.WriteLine(“The number is not prime”);
  20.                     break;
  21.                 }
  22.             }
  23.             if (i==number)
  24.             {
  25.                 Console.WriteLine(“The number is prime”);
  26.             }
  27.             Console.ReadKey();
  28.         }
  29.     }
  30. }

Download Code

Advertisements
Categories: Math-Snippets | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.