Social Icons

Wednesday, 12 February 2014

Program to generate prime numbers within given range

using System;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int l, h, i, c;
            Console.Write("\n Enter the lowest range : ");
            l = int.Parse(Console.ReadLine());
            Console.Write("\n Enter the highest range : ");
            h = int.Parse(Console.ReadLine());
            Console.Write("\n Prime numbers are : ");
            while (l <= h)
            {
                c = 0;
                for (i = 2; i <= l; i++)
                {
                    if (l % i == 0)
                        c++;
                }
                if (c < 2)
                    Console.Write("  " + l);
                l++;
            }
            Console.ReadLine();
        }
    }
}


                                                            Output



No comments:

Post a Comment