On entry to the function it increments a static variable. If a variable has a value below 100, it calls itself again. It then prints the value of the variable, decrements it and returns
How do you Write a Program using Recursive Function that prints a list of numbers from 100 down to 1 in C?
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
void printnum(int num)
{
printf("%d",num);
if(num%26gt;1)
printnum(num-1);
}
int main()
{
printnum(100);
getch();
return 0;
}
Reply:void fun(int num)
{
printf("%d",num);
if (n%26gt;1)
{
fun(num-1);
}
}
main()
{
fun(100);
}
Reply:void printnum(int num, int hasnextline)
{
if (num %26gt; 0)
{
printf("%d", num);
if (hasnextline)
{
printf("\n");
}
printnum(num - 1, hasnextline);
}
return;
}
That should do it... But I haven't tried this one... Recursive functions are rather difficult to create, since this one is a bit basic, it'll be a bit easy to understand...
I hope this code works... (^^,)
BTW, int hasnextline is the variable which tells the function if you need to print a next line character after every number... Which somehow adds a feature for the function...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment