In this article we
will discuss with you the program for getting the first N prime number using
console application in c#.net:
Step1: Take a new console application using c# as
a language in the Visual studio.
Step2: Read an input from the User in this
Variable “int_Number” for storing how many prime number you want to generate.
Step3: Now if that input
number is 1 then the simply prime number will be 1 or if input number is 2 then
the prime number will “1 and 2” otherwise we have to compute it through the
below step
- We will test the values starting from 3 and loop till we get the “prime no count” is equal to the “input number”.
- Here I have assigned “int_PrimeNo_Count” to 2 since 1 and 2 is first two prime nos.
- Now we have to loop from 2 till that number which we are testing for prime number and Start dividing that number from 2 till that number, If it’s divisible by any of the number before that i.e. “number-1” then it means that it’s not a prime number otherwise it is a prime number.
- If it is prime number then increment the “int_PrimeNo_Count” and add it to the list of prime no in the string variable “str_PrimeNO”.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace First_N_Prime
{
    Class Program
    {
        static void Main()
        {
            int int_Number;
            int
i, int_PrimeNo_Count;
            string
str_PrimeNO;
            str_PrimeNO = "1 2";
            Console.WriteLine("Please enter how many prime no you want to
print?");
            int_Number = int.Parse(Console.ReadLine());
            if
(int_Number == 1)
            {
                str_PrimeNO = "1";
            }
            else
if (int_Number == 2)
            {
                str_PrimeNO = "1 2";
            }
            else
            {
                //Start
dividing the value from 3 i.e. the value of j and increment j 
                 // value till we get  int_PrimeNo_Count = int_Number     
                int
j = 3;
                //we
have 1 and 2 as prime no hence count is 2 and increment it when you  
                 // get the next prime no
                int_PrimeNo_Count = 2;
                //till
you have n no of prime number
                while
(int_PrimeNo_Count != int_Number)
                {
                    for
(i = 2; i <= j; i++)
                    {
                        //Divide that
number starting with 2 till that number
                        if (j % i == 0)
                        {
                            break;
                        }
                    }
                    //if
it is the last number then means that it's a prime no,
                    //add
it to the prime no list and increment the primeNo counter
                    if
(j == i)
                    {
                        str_PrimeNO =
str_PrimeNO + " " + i;
                        int_PrimeNo_Count++;
                    }
                    j++;
                }
            }
            Console.WriteLine(str_PrimeNO);
            Console.ReadLine();
        }
    }
}
Output:-

 
No comments:
Post a Comment