Prime Number Program in C#


Prev Tutorial Next Tutorial

Prime Number Program in C#

Prime number is only divisible by 1 and number it self. Here we write code for this in C#. To display any message on screen we use WriteLine() the static method of Console class.

Prime Number Program in C#

using System;
namespace example
{
class Prime_Number
{
public static void Main()
{
Console.Write("Enter a Number : ");
int num;
num = Convert.ToInt32(Console.ReadLine());
int k;
k = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
 k++;
 }
 }
if (k == 2)
{
Console.WriteLine("Entered Number is a Prime Number and the Largest Factor is {0}",num);
}
else
{
Console.WriteLine("Not a Prime Number");
}
Console.ReadLine();
}
}
}

Output

Enter a Number : 23
Entered Number is a Prime Number and the Largest Factor is 23

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements