C# : Program to check whether number is prime or not

In this C# tutorial we were checking whether number is prime or not . Here we are taking two variables "num" for number and and "a" for counter variable. Now, lets get started!!

Note: Prime number is the number which divides by itself for e.g 2, 7, 11, 17....

using System;
​class prime
{
 public static void Main(String []args)
 {
        int num;
        Console.WriteLine("Please enter number : ");
        num=Convert.ToInt32(Console.ReadLine());
        for (int a = 2; a <= num / 2; a++)
     {
        if (num % a == 0)
        {
            Console.WriteLine(num + " is not prime number");
            return;
        }

    }
            Console.WriteLine(num + " is a prime number");
 }
}
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment