recursion
#include <stdio.h>
void recursiveFunction(int n);
int main() {
int num = 5;
recursiveFunction(num);
return 0;
}
void recursiveFunction(int n) {
if (n > 0) {
printf("%d\n", n);
recursiveFunction(n - 1);
}
}
The program starts by including the necessary header file (
stdio.h
), which is required for input and output operations.The program defines a recursive function named
recursiveFunction
that takes an integer parametern
. This function will print the value ofn
and then call itself withn - 1
ifn
is greater than 0.The
main
function is the entry point of the program. It declares an integer variablenum
and initializes it with the value 5.The
main
function then calls therecursiveFunction
with the value ofnum
as an argument.Inside the
recursiveFunction
, there is a base case check (if (n > 0)
) to determine whether the recursion should continue. Ifn
is greater than 0, it prints the value ofn
and calls itself withn - 1
.The recursive calls continue until the base case is met (i.e.,
n
becomes 0), at which point the recursion stops, and the function returns without making another recursive call.The output of the program will be:
5 4 3 2 1
This demonstrates how the recursive function counts down from the initial value of num
(5 in this case) to 1.