Pages

Wednesday, April 3, 2013

Prime Number program in C#

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

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j = 2, flag = 1;
            Console.WriteLine("Enter a Number");
            i =Convert.ToInt16(Console.ReadLine());
            while (j <= i / 2)
            {
                if (i % j == 0)
                {
                    flag = 0;
                    break;
                }
                else
                {
                    j++;
                }
            }
            if (flag == 0)
                Console.WriteLine("Given Number is not a Prime");
            else
                Console.WriteLine("Given Number is Prime");
            Console.ReadKey();
        }
    }
}

0 comments:

Post a Comment