Prime Number in C Sharp

Prime Number

A Prime number is a number that can be divided either by itself or 1.

For example,

2, 3, 5, 7, 11, 13, 17, 19, 23, etc.

Example:

using System;  
  public class Example  
   {  
     public static void Main(string[] args)  
      {  
          int num, i, x=0, f=0;    
          Console.Write("Enter a Number: ");    
          num = int.Parse(Console.ReadLine());  
          x = num/2;    
          for(i = 2; i <= x; i++)    
          {    
           if(num % i == 0)    
            {    
             Console.Write("Number is not Prime.");    
             f=1;    
             break;    
            }    
          }    
          if (f==0)    
           Console.Write("Number is Prime.");       
     }  
   }

Output:

Explanation:

In the above example, we are displaying the prime number program in C#. Here, we are taking input from the user. The above code then checks whether the entered number is prime or not.