Easy Tutorial
❮ C Program Structure C Error Handling ❯

C Language Example - Josephus Survivor and Casualty Game

C Language Examples

30 people are on a boat, which is overloaded, and 15 need to get off.

So, people line up in a queue, and their positions in the queue are their numbers.

They count off, starting from 1, and every 9th person steps off the boat.

This continues until only 15 people remain on the boat. Which numbers have stepped off the boat?

Example

#include<stdio.h>

int c = 0;
int i = 1;
int j = 0;
int a[30] = { 0 };
int b[30] = { 0 };

int main() {
    while (i<=31) {
        if (i == 31) {
            i = 1;
        } else if (c == 15) {
            break;
        } else {
            if (b[i] != 0) {
                i++;
                continue;
            } else {
                j++;
                if (j != 9) {
                    i++;
                    continue;
                } else {
                    b[i] = 1;
                    a[i] = j;
                    j = 0;
                    printf("Person number %d has left the boat\n", i);
                    i++;
                    c++;
                }
            }
        }
    }
}

Executing the above example, the output is:

Person number 9 has left the boat
Person number 18 has left the boat
Person number 27 has left the boat
Person number 6 has left the boat
Person number 16 has left the boat
Person number 26 has left the boat
Person number 7 has left the boat
Person number 19 has left the boat
Person number 30 has left the boat
Person number 12 has left the boat
Person number 24 has left the boat
Person number 8 has left the boat
Person number 22 has left the boat
Person number 5 has left the boat
Person number 23 has left the boat

C Language Examples

❮ C Program Structure C Error Handling ❯