Author : Rahul
Write a program to find whether entered number is prime or not
Prime Number:
A number which is divisible by 1 and itself is said to be a prime number.
Program:
using System;
namespace ProgrammingQuestion
{
class Program
{
static void Main(string[] args)
{
//Variable Declaration
string checkVal, message;
int checkNum;
bool isPrime = true;
//User Input
Console.Write("Enter Number:");
checkVal = Console.ReadLine();
checkNum = Convert.ToInt32(checkVal);
//Prime Logic
for (int i = 2; i <= checkNum / 2; i++)
{
if (checkNum % i == 0)
{
isPrime = false;
break;
}
}
//Print Result
if(isPrime)
{
message = $"Entered Number {checkNum} is a prime number.";
}
else
{
message = $"Entered Number {checkNum} is not a prime number.";
}
Console.WriteLine(message);
Console.Read();
}
}
}
Output:
Enter Number:15
Entered Number 15 is not a prime number.