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?
1
Expert's answer
2012-09-28T10:40:13-0400
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!

Comments

Assignment Expert
21.01.14, 19:04

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

patel hardik k
17.01.14, 11:57

very nice code good

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS