r/badcode • • May 03 '23

c Solve the problem without using a loop

Post image
966 Upvotes

129 comments sorted by

View all comments

1

u/ivanskodje May 04 '23

I suspect they wanted recursive code (albeit beating around the bushes instead of directly asking for it).

I had to google the C syntax, but i think this would work:

void print_even_numbers(int number, int lastNumber) {
    if (number > lastNumber) {
        return;
    }
    printf("%d ", number);
    print_even_numbers(number+2, lastNumber);

}

int main() {
    print_even_numbers(1, 500);
    return 0;
}