All Type Coding

Search Here

How to Find 1 to n Prime Numbers in c#.net.?


using System;

class Program
{
    static void Main(string[] args)
    {
        int i, j, n = 0, n1 = 0;
        Console.WriteLine("Enter The n value");
        n = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Prime Numbers are:");
        Console.WriteLine("\n");
        for (i = 2; i <= n; i++)
        {
            for (j = 1; j <= i; j++)
            {
                if (i % j == 0)
                {
                    n1++;
                }
            }
            if (n1 == 2)

                Console.WriteLine(i);
            n1 = 0;
        }
        Console.ReadLine();
    }
}
Output:
















Description: 
Prime Number:If a number is divisible by 1 and itself is known as a PrimeNumber.Remember 2 is termed as even primenumber


No comments :

Post a Comment