How write a recursive print program

前端 未结 8 2076
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 16:02

Gurus,

I want to know how to write a recursive function that prints

1
12
123
1234
...
......

For eg: display(4) should print

8条回答
  •  长发绾君心
    2021-01-29 16:39

    EDIT: OK, I improved my answer with the guidelines of @lc.

    void print_recursive(unsigned int num) {
        if (num > 1) {
            print_recursive(num - 1);
        }
        for (unsigned int i = 0; i < num; i++) {
            printf("%d ", (i + 1));
        }
        printf("\n");
    }
    

提交回复
热议问题