A prime number is a numerical value exceeding 1 that can only be evenly divided by 1 or itself. Put differently, prime numbers are indivisible by any other number except 1 or themselves. Instances of prime numbers include 2, 3, 5, 7, 11, 13, 17, 19, 23, and so forth.
Let's examine the C# code for determining prime numbers. Within this C# script, we'll gather user input and verify if the given number is a prime number.
Example
Example
using System;
public class PrimeNumberExample
{
public static void Main(string[] args)
{
int n, i, m=0, flag=0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
Console.Write("Number is not Prime.");
flag=1;
break;
}
}
if (flag==0)
Console.Write("Number is Prime.");
}
}
Output:
Output
Enter the Number to check Prime: 17
Number is Prime.
Example
Enter the Number to check Prime: 57
Number is not Prime.