C Example: Josephus Problem: counting eliminations, array shifts and survivor
Simulates n people in a circle with an array: each round eliminates the k-th person, shifts later elements forward, and wraps the index modulo the remaining count; repeats until one remains. Solves (10,3), (41,3) and (7,2), printing elimination order and survivor.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
n=10 k=3 eliminated: 3 6 9 2 7 1 8 5 10 survivor: 4 (after 9 eliminations) n=41 k=3 eliminated: 3 6 9 12 15 18 21 24 27 30 33 36 39 1 5 10 14 19 23 28 32 37 41 7 13 20 26 34 40 8 17 29 38 11 25 2 22 4 35 16 survivor: 31 (after 40 eliminations) n=7 k=2 eliminated: 2 4 6 1 5 3 survivor: 7 (after 6 eliminations)
🧩 Keep Learning