Wednesday 12 August 2015

Given Number is Prime number or not in C#.Net

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

namespace StringReversewithalternativecharacters
{
  public  class PrimeNumberornot
    {
      public static void Main()
      {
          int count = 0;
          Console.Write("Enter a Number :");
          int x = int.Parse(Console.ReadLine());
          for (int i = 2; i <= x/2; i++)
          {
              if (x%i==0)
              {
                  count++;
                  break;
              }
          }
          if (count == 0 && count != 1)
          {
              Console.WriteLine(x + " is Prime Number");
          }
          else
          {
              Console.WriteLine(x + " is not Prime Number");
          }
          Console.ReadLine();
     }     
    }
}

                                                                    (OR)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringArray
{
 public    class PrimeNumberorNot
    {
        public static void Main()
        {
            int count = 0;

            int x = 5;
            int y = 2;
            if ( y <= x/2)
            {
                if (x % y == 0)
                {
                 count++;
                }            
            }

            if (count == 0 && count != 1)
            {
                Console.WriteLine(x + " is Prime Number");
            }
            else
            {
                Console.WriteLine(x + " is not Prime Number");
            }
            Console.ReadLine();
        }
 }

}






No comments:

Post a Comment