Prime Number in C#

Today in this article I will share you how to create Prime number in C#. This is the common question of any interviewer if you are fresher. So I explain in details what is prime number and how to check the number is prime or not.

What is Prime Number?
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 
Example:


    using System;
    namespace PrimeNumber
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a number");
                int number = Convert.ToInt32(Console.ReadLine());
                int result = Check_Prime(number);
                if (result == 0)
                {
                    Console.WriteLine("{0} is not a prime number", number);
                }
                else
                {
                     Console.WriteLine("{0} is  a prime number", number);
                }            
                Console.Read();
            } 
    
            private static int Check_Prime(int number)
           {
                int i;
                for (i = 2; i <= number - 1; i++)
                {
                    if (number % i == 0)
                    {
                        return 0;
                    }
                }
                if (i == number)
                {
                    return 1;
                }
                return 0;
            }
        }  
    }
Output:
Enter the Number to check Prime: 13
Number is Prime.   

Enter the Number to check Prime: 41
Number is not Prime.

Points to Remember

  • The only even prime number is 2. All other even numbers can be divided by 2.
  • If the sum of a number's digits is a multiple of 3, that number can be divided by 3.
  • No prime number greater than 5 ends in a 5. Any number greater than 5 that ends in a 5 can be divided by 5.
  • Zero and 1 are not considered prime numbers.
  • Except for 0 and 1, a number is either a prime number or a composite number. A composite number is defined as any number, greater than 1, that is not prime.

To prove whether a number is a prime number, first try dividing it by 2, and see if you get a whole number. If you do, it can't be a prime number. If you don't get a whole number, next try dividing it by prime numbers: 3, 5, 7, 11 (9 is divisible by 3) and so on, always dividing by a prime number (see table below).
Here is a table of all prime numbers up to 1,000:
23571113171923
29313741434753596167
717379838997101103107109
113127131137139149151157163167
173179181191193197199211223227
229233239241251257263269271277
281283293307311313317331337347
349353359367373379383389397401
409419421431433439443449457461
463467479487491499503509521523
541547557563569571577587593599
601607613617619631641643647653
659661673677683691701709719727
733739743751757761769773787797
809811821823827829839853857859
863877881883887907911919929937
941947953967971977983991997

Hope It will clear about the Prime Number. Still have any question regarding this, you can add the comments on below Disqus Forum.