Finding a Prime Number

Finding a prime number for a specific number

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

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the number to find out if its prime number or not : ");
            int number = Convert.ToInt32(Console.ReadLine());
            int counter = 0;
            for (int i = 2; i <= number; i++)
            {
                if (number % i == 0)
                {
                    counter = counter + 1;
                }
                if (counter > 1)
                    break;
            }
            if (counter <= 1)
            {
                Console.WriteLine("The number {0} is a Prime Number", number);
            }
            else
            {
                Console.WriteLine("The number {0} is Not a Prime Number", number);
            }
            Console.ReadKey();
        }
    }
}

Output :


Finding a prime numbers from a set of numbers (within min and max range)

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

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {  
            Console.WriteLine("Enter the minimum number : ");
            int minnumber = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the maximum number : ");
            int maxnumber = Convert.ToInt32(Console.ReadLine());

            if ((minnumber < 1) || (maxnumber <= 1) || (minnumber >= maxnumber))
            {
                Console.WriteLine("Either the min number is less than 1 or max number is less than or equal to 1 or min number is greater than max number. Cannot Continue.. Exiting.");
                Console.ReadKey();
                return;
            }
         
            for (int number = minnumber; number <= maxnumber; number++)
            {
                int counter = 0;
                    for (int i = 2; i <= number; i++)
                        {
                            if (number % i == 0)
                                {
                                counter = counter + 1;
                                }
                            if (counter > 1)
                                break;
                        }
                if (counter <= 1)
                    {
                        Console.WriteLine("The number {0} is a Prime Number", number);
                    }
                else
                    {
                        Console.WriteLine("The number {0} is Not a Prime Number", number);
                    }           
            }
            Console.ReadLine();
        }
    }
}

Output :

No comments: