simple Recursion program - strange output
i want to write program which prints all natural numbers from 1 to n.
where n is user input. but i want to write using recursion and not by for loop.
ex. if user enters 5 then output should be 1 2 3 4 5
i tried following code but its not working.
Code:
#include <stdio.h>#include <stdlib.h>
int main()
{
int n;
printf("enter no.");
scanf("%d", &n);
rec(n);
return 0;
}
int rec(int n) {
if(n==1) {
printf("1\n");
}
else {
n--;
printf("%d\n",rec(n));
}
}
the output is 1 0 2 2 2....upto n.
please help me and can anyone tell me whats the mistake in this code
thanks