# Answer to Question #14763 in C# for Miths

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();

& }

}

}

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();

& }

}

}

## Comments

Assignment Expert21.01.14, 19:04Dear Patel Hardik K,

You're welcome. If you liked our service please press like-button beside answer field. Thank you!

patel hardik k17.01.14, 11:57very nice code good

## Leave a comment