Saturday 16 April 2016

Write a Program to Check Whether inputted No. is Prime or Not in C#.

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

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, i,flag=0;
            Console.WriteLine("Enter No. : ");
            n = int.Parse(Console.ReadLine());
            for (i = 2; i <= n / 2; ++i)
            {
                if (n % i == 0)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
                Console.WriteLine(n+" is a prime number.");
            else
                Console.WriteLine(n+" is not a prime number.");
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment