I'm writing a program that prints out the first 10 natural numbers in recursion. First off, I'm putting a parameter "--num"
int natural_numbers(int num) {
if (num > 1) // Base case
natural_numbers(--num);
printf("%d ", num);
}
Input: 10
Output: 1 1 2 3 4 5 6 7 8 9
And then I changed the parameter to "num - 1" and it prints out what I was expected.
int natural_numbers(int num) {
if (num > 1) // Base case
natural_numbers(num - 1);
printf("%d ", num);
}
Input: 10
Output: 1 2 3 4 5 6 7 8 9 10
I have no idea why the first one ouput was wrong. I need some help.