Question #14763

A program to accept a number from user and display all the prime numbers from zero up to the number entered by user?

Expert's answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{

& static void Main(string[] args)
& {
Console.WriteLine("Enter number");
int n = int.Parse(Console.ReadLine()); //get N
bool[] A = new bool[n];
//Initialization of default array from 1 to N
for (int i = 2; i < n; i++)
{
A[i] = true;
&
}
//Searching all prime numbers
for (int i = 1; i < Math.Sqrt(n) + 1; ++i)
{
if (A[i])
{
& for (int j = i * i; j < n; j += i)
& {
A[j] = false;
& }
}
}
Console.WriteLine("Results: ");
// Output
for (int i = 2; i < n; i++)
{
if (A[i]) &
& Console.Write("{0} ", i);
}
Console.ReadKey();

& }
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS