C# Program to print prime numbers from 1 to N

In this C# program, we will take input from the user and print prime numbers between 1 and the number input by the user. A prime number is a positive integer that is divisible only by 1 and itself.

 

C# Program to print prime numbers from 1 to N Code:

 private static void Main(string[] args)
        {
            bool isPrimeNo = false;
            int j;
            Console.WriteLine("Enter the nth No");
            int nthNo = Int32.Parse(Console.ReadLine());
            Console.Write("Prime numbers between 1 and {0} are: ", nthNo);
            for (int i = 2; i <= nthNo; i++)
            {
                for (j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        isPrimeNo = true;
                        break;
                    }
                }

                if (!isPrimeNo)
                    Console.Write("{0} ", j);
                else
                    isPrimeNo = false;
            }

            Console.ReadLine();
        }

C# Program to print prime numbers from 1 to N Output:

C#-Program-to-print-prime-numbers-from-1-to-N

 
We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
3 + 8 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2022 CodingFusion
75 Important SQL Server queries you should know