How To Find Prime Number Between 1 To N

less than 1 minute read

class Program
{
    static void Main(string[] args)
    {
        //int n = 100000;
        int n = 0;
        Console.WriteLine("Type an integer number to get prime number between 1 to n");
        Int32.TryParse(Console.ReadLine(), out n);
        PrintPrimes(n);
    }

    public static void PrintPrimes(int n)
    {
        bool isPrime = false;
        for (int i = 2; i < n; i++) 
        { 
            isPrime = true; for (int j = 2; j <= Math.Sqrt(i); j++) 
            { 
                if (i % j == 0) 
                { 
                    isPrime = false; break; 
                } 
            } 
            if (isPrime) 
                Console.Write(i + " "); 
        } 
        Console.ReadKey(); 
    }
} 

Tags:

Categories:

Updated:

Comments