Medium csharp

Check if 13 is prime

Problem

Print Prime or Not Prime for 13.

Hints
  • Loop d while d*d <= n

Your practice code

Ready — edit the code above and click Run.

Solution

using System;

class Program
{
    static void Main()
    {
        int n = 13;
        bool ok = n > 1;
        for (int d = 2; d * d <= n && ok; d++)
            if (n % d == 0) ok = false;
        Console.WriteLine(ok ? "Prime" : "Not Prime");
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Trial division to sqrt(n).

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details