How to Solve the Code Whether Entered Number is Prime or Not

Dec 10 2013 8:07 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Console1
{
    class PrimeNumber
    {
        static void Main(string[] args)
        {
           int num,a;
           int flag;
           Console.WriteLine("Enter any number");
           num = int.Parse(Console.ReadLine());

           if (num == 0 || num == 1)
           {
               Console.WriteLine("Given Number is Not a Prime Number");
           }
           else
           {
               for (a = 2; a <= num; a++)
               {
                   flag = 0;
                   if (num % a == 0)
                       flag = 1;
                       //Console.WriteLine("{0} is Not a Prime Number", num);
                   return;
               }
               //Console.WriteLine("{0} is a Prime Number", num);
               if (flag == 1)
               {
                   Console.WriteLine(num + " is not Composite Number");
               }
               else
                   Console.WriteLine(num + "is a Prime Number");
           }

           /*if (flag == 1)
           {
               Console.WriteLine(num + " is not Composite Number");
           }
           else
               Console.WriteLine(num + "is a Prime Number");*/
        
        }
    }
}
 

Answers (2)