Josephus Problem in C#

Published on 06 November 2024 (Updated: 13 May 2026)

Welcome to the Josephus Problem 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 nText, var kText]
    || !int.TryParse(nText, out int n)
    || !int.TryParse(kText, out int k)
    || n <= 0
    || k <= 0
)
{
    Console.Error.WriteLine(
        "Usage: please input the total number of people and number of people to skip."
    );
    return;
}

int survivor = 0;
for (int i = 2; i <= n; i++)
    survivor = (survivor + k) % i;

Console.WriteLine(survivor + 1);

Josephus Problem 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.