Prime Number Program in C#

Advertisements

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 Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements