Wednesday 19 August 2015

Prime Number program in C#.NET

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

namespace Primenumber
{
    class Class1
    {
        static void Main(string[] args)
        {
            bool current = false;
            int j;

            Console.WriteLine("Prime numbers upto 1000");

           // int num = Int32.Parse(Console.ReadLine());

            for (int i = 2; i <= num ; i++)   //2 is the first prime number.
            //I set i to 2 beacuse it has to print 2 firstly...
            {
                for (j = 2; j < i; j++)
                {
                    if (i % j == 0)//Controls i is prime number or not...
                    {
                        current = true;
                        break;//breaks for not controlling anymore...
                    }
                }
                if (current == false)
                    Console.Write("{0} ", j);//if i is prime number, print it...
                else
                    current = false;
            }

            Console.ReadLine();
        }
        }
    }
OUTPUT:

   2  3  5  7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97   

No comments:

Post a Comment