C# Function to check Prime Number

What is Prime Number?

A prime number is a number that is greater than 1 and can be divided only by 1 and itself.

For ex. 3, 5, 7… are  prime numbers.

why?

  1. Because they are greater than 1 And
  2. They can be divided only by 1 and itself.

But, 4,6,8,….are not prime numbers, because they don’t follow the two rules given above.

 

How to find a Prime Number in C#?


public static bool IsPrimeNumber(int number)
{
    int i;
    for (i = 2; i < number; i++)
    {
        if (number % i == 0)
            return false;
    }

    return (i == number);
}

Done..!!