Console Application For Finding Prime No's From Range

Introduction

In this blog we have created a Simple C# Console Application that accepts Range from the user and gives prime no's from the given range.

Step 1: Create New Console Application in Visual Studio 2010.

File Menu -> New ->New Project -> Console Application

Image1
 
Step 2: Write the Following Code in "Program.cs" File.

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace PrimeNo_C

{

    class Program

    {

        static void Main(string[] args)

        {

            int st, en, cnt = 0;

            Console.WriteLine("Starting Value:-");

            st = int.Parse(Console.ReadLine());

            Console.WriteLine("Ending Value:-");

            en = int.Parse(Console.ReadLine());

 

            for (int i = 1; i < en; i++)

            {

                for (int j = 1; j < en; j++)

                {

                    if (i % j == 0)

                    {

                        cnt++;

                    }

                }

                if (cnt == 2)

                {

                    Console.WriteLine(i);

                }

                cnt = 0;

            }

            Console.ReadKey();

        }

    }

}
 
Output

Image2

Summary

We Get the prime No's Between the Given Range that Accepts from the user.

Next Recommended Reading Use Log4Net in C# Console Application