Possible Duplicate:
How write a recursive print program
I have already asked this question, but it was closed because of insufficient information.
How write a recursive print program
Gurus,
I want to know how to write a recursive function that prints
1
12
123
1234
…
……
For eg: display(4) should print
1
12
123
1234
Code
#include <stdio.h>
void print(int n)
{
if(n != 0)
{
print(n-1);
printf("\n");
print(n-1);
printf("%d",n);
}
}
int main()
{
print(3);
}
Output
1
12
123
Issues
I wanted to write a pure recursive function but unable to filter unwanted prints.
Hope someone will help me out!!!
May this be what you want?
Compile it and execute like this:
or without any argument (which defaults to
display 8):The trick is to call
print(n-1)before printing the output for the currentn. This is the program flow when executed asdisplay 3:This is what gets printed to the console:
If you move
print(n-1)afterprintf("\n")you will have: