Prime Number in C#

Q. 1 What is Prime Number?

Answer:  A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 

Q. 2 Write a programmer in C# to check number is prime or not ?

Answer: The following code snippet to check prime number or not.

  1. using System;  
  2.   
  3. namespace PrimeNumber  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Enter a number");  
  10.             int number = Convert.ToInt32(Console.ReadLine());  
  11.             int result = Check_Prime(number);  
  12.             if (result == 0)  
  13.             {  
  14.                 Console.WriteLine("{0} is not a prime number", number);  
  15.             }  
  16.             else  
  17.             {  
  18.                  Console.WriteLine("{0} is  a prime number", number);  
  19.             }              
  20.             Console.Read();  
  21.         }   
  22.   
  23.         private static int Check_Prime(int number)  
  24.        {  
  25.             int i;  
  26.             for (i = 2; i <= number - 1; i++)  
  27.             {  
  28.                 if (number % i == 0)  
  29.                 {  
  30.                     return 0;  
  31.                 }  
  32.             }  
  33.             if (i == number)  
  34.             {  
  35.                 return 1;  
  36.             }  
  37.             return 0;  
  38.         }  
  39.     }    
  40. }