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: