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();
Comments
Dear Patel Hardik K, You're welcome. If you liked our service please press like-button beside answer field. Thank you!
very nice code good
Leave a comment