Q. 1 What is Prime Number?
Answer: A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Q. 2 Write a program in C# to check number is prime or not ?
Answer: The following code snippet to check prime number or not.
using System;
namespace PrimeNumber
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number");
int number = Convert.ToInt32(Console.ReadLine());
bool isPrime = IsPrime(number);
if (isPrime)
{
Console.WriteLine("{0} is a prime number", number);
}
else
{
Console.WriteLine("{0} is not a prime number", number);
}
Console.Read();
}
private static bool IsPrime(int number)
{
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}
}