C# Examples
C# ExamplesPrograms with output

C# Program to Check Number is Prime or not


Example:
using System;  
public class CheckPrimeOrNot  
{  
    public static void Main(string[] args)  
    {  
        int n, i, m=0, flag=0;    
        Console.Write("Enter any Number: ");    
        n = int.Parse(Console.ReadLine());  
        m=n/2;   
        
        for(i = 2; i <= m; i++)    
        {    
            if(n % i == 0)    
            {    
                Console.Write("\nIt is Prime Number");    
                flag=1;    
                break;    
            } 
        }    
        if (flag==0)    
           Console.Write("\nIt is not Prime Number");       
    }  
}
Output:
Enter any Number:
15
It is Prime Number

Share this page on:

Was this page helpful ?

Let us know how we did!

Subscribe Email Updates

to get latest update