C# Program to Check a Number is Prime Number or not

Introduction :

Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can’t be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17…. are the prime numbers.

Here is source code of the C# Program to Check a Number is Prime Number or not, if so then Display its Largest Factor. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

C# Program to Check a Number is Prime Number or not

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace prime
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n;
            Console.WriteLine("Enter the Number:");
            n = Convert.ToInt32(Console.ReadLine());
              
            for (i = 2; i <=2; i++) { if (n % i == 0) break; } if (i > 2)
            {
                Console.WriteLine("prime number:", n);
            }
            else
            {
                Console.WriteLine("Not Prime Number:", n);
            }
        }
    }
}

OutPut of Program :

Enter the Number : 17  
Number is Prime.  
Enter the Number : 22 
Not Prime Number  

 

Learn Recommended posts :

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *