Prime number program in C#

A prime number can be divided evenly only by 1, or itself and it must be a whole number greater than 1. A number divide by itself, called prime number.

Example of prime number:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, etc.

Program of prime number in C# console application


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace prime_number
    {
        class Program
        {
            static void Main(string[] args)
            {
                int loop, number;
                Console.WriteLine("ENTER THE NUMBER FOR PRIME :- ");
                number = Convert.ToInt32(Console.ReadLine());
                for (loop = 2; loop < number; loop++)
                {
                    if (number % loop == 0)
                    {
                        Console.WriteLine("ENTER NUMBER IS NOT PRIME.");
                    }
                    
                    else
                    {
                        Console.WriteLine("ENTER NUMBER IS PRIME.");
                    }
                    break;
                }
                Console.ReadLine();
            }
        }
    }

Output:

	
   ENTER THE NUMBER FOR PRIME :- 5
   ENTER NUMBER IS PRIME.
           

Post Your Comment