Trending News
How do you Write a Program using Recursive Function that prints a list of numbers from 100 down to 1 in C?
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
the description above is merely a hint, but I need help figuring the program from scratch
3 Answers
- 1 decade agoFavorite Answer
#include<stdio.h>
#include<conio.h>
void printnum(int num)
{
printf("%d",num);
if(num>1)
printnum(num-1);
}
int main()
{
printnum(100);
getch();
return 0;
}
Source(s): Practice - 1 decade ago
void printnum(int num, int hasnextline)
{
if (num > 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...
- 1 decade ago
void fun(int num)
{
printf("%d",num);
if (n>1)
{
fun(num-1);
}
}
main()
{
fun(100);
}