How to Find First 500 Prime Numbers in C#

This is the simple but logic building program for beginners. The program will find out the first 500 prime number. e.g. 2 3 5 7 11 13 are the first 6 prime numbers.

Source Code 

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

namespace assad
{
class firstPrimes
{
static void Main(string[] args)
{
welcomeScrean();
double n;
int count=0;
int countLimit=500;// by setting this limit we can find first 500 prime numbers

for (n = 2; count < countLimit; n++) {
double limit = Math.Sqrt(n);
int i;
for (i = 2; i <= limit; i++) {
if (n % i == 0) {
break;
}
}

if (i > limit) {
if (count % 13 == 0) { Console.Write("\n\t"); }
Console.Write(n + " "); count++;
}
}
int ab = Console.Read();
}
private static void welcomeScrean()
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("\t*******************************************************" + "\n"
+ "\t*******************************************************" + "\n"
+ "\t** Finding First **" + "\n"
+ "\t** 500 Prime Numbers **" + "\n"
+ "\t** Program **" + "\n"
+ "\t*******************************************************" + "\n"
+ "\t*******************************************************\n\n\n\n");
}
}
}


Output of the Program

 Find First 500 Prime Numbers in C#

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment