Programe to find Prime number in between range in C#.

Hi All, this is very simple program but I was getting bored so I am posting it 😛

We have two numbers n1 and n2. And we need to find all the prime numbers in between these two.

using System;

namespace ConsoleApplication
{
    class Odd_Even_Numbers
    {
        public static void Main()
        {
            int n1, n2;
            string nPrime = string.Empty;
            Console.WriteLine(“enter first number: “);
            n1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(“enter second number: “);
            n2 = Convert.ToInt32(Console.ReadLine());
    FindPrimeNumber(n1, n2, nPrime);
        }

        private static void FindPrimeNumber(int n1, int n2, string nPrime)
        {
            bool flag = true;
            for (; n1 <= n2; ++n1)
            {
                flag = true;
                for (int i = 2; i <= n1 / 2; ++i)
                {
                    if (n1 % i == 0)
     {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    nPrime += n1.ToString() + “, “;
                }
            }
            Console.WriteLine(“Prime numbers are: ” + nPrime);
            Console.ReadLine();
        }
    }
}

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s