Check if a number is prime number with C# programming language

On the post we going to write a simple program to check either the number given is prime number or not. The number will be read on Console.

As we already familiar with in mathematics, prime number is any positive integer number that cannot be divided by another number except 1 and itself. to check either given number is a prime number not using C# program, just copy a paste these below on your development environment i.e visual studio.

Console.WriteLine(“Hello World!”);

Console.WriteLine(“Enter as positve number: ” );

int num = int.Parse(Console.ReadLine());

int divider = 2;

int maxDivider = (int)Math.Sqrt(num);

bool prime = true;

while (prime && (divider <= maxDivider))

{

if (num % divider == 0)

{

prime = false;

}

divider++;

}

Console.WriteLine(“prime? ” + prime);

When run these code above, it will prompt us to enter positive number on the console window shown below

Now as we enter positive number  (37) on the above page and heat enter, it will show  us prime number is  true.

now to either the prime is false, enter 34, a page show will display telling us that prime number is false

As we can see on the above page, it show that prime number is false, while because we enter 34 which is not a prime number.

That is all for these tutorial we will bring video that explain everything teach later.

Thanks for reading, have any question or comment drop on our comment section below.

See also  Kogi Governor Wife, others Commission Transformer Donated By Husband to Ohono community

Be the first to comment

Leave a Reply

Your email address will not be published.


*