A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
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.
using System;
namespace JosephusProblem
{
class Program
{
const string Usage = "Usage: please input the total number of people and number of people to skip.";
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine(Usage);
return;
}
if (!int.TryParse(args[0], out int n) || !int.TryParse(args[1], out int k) || n <= 0 || k <= 0)
{
Console.WriteLine(Usage);
return;
}
int survivor = FindJosephusPosition(n, k);
Console.WriteLine(survivor);
}
static int FindJosephusPosition(int n, int k)
{
int result = 0;
for (int m = 2; m <= n; m++)
{
result = (result + k) % m;
}
return result + 1;
}
}
}
Josephus Problem in C# was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.