Prime Number in C#

Published on 30 December 2018 (Updated: 13 May 2026)

Welcome to the Prime Number in C# page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

if (args is not [var raw] || !ulong.TryParse(raw, out ulong number))
    return ExitWithUsage();

Console.WriteLine(IsPrime(number) ? "Prime" : "Composite");
return 0;

static bool IsPrime(ulong value)
{
    if (value < 2)
        return false;

    if (value == 2)
        return true;

    if (value % 2 == 0)
        return false;

    for (ulong divisor = 3; divisor * divisor <= value; divisor += 2)
    {
        if (value % divisor == 0)
            return false;
    }

    return true;
}

static int ExitWithUsage()
{
    Console.WriteLine("Usage: please input a non-negative integer");
    return 1;
}

Prime Number in C# was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.