Write a recursive function that calculates the factorial of a positive integer.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
int factrec(int n)
{
if (n <= 1)
return 1;
return n * factrec(n-1);
}
void main()
{
int n;
printf("n : ");
scanf("%d", &n);
printf("%d!=%d\n",n,factrec(n));
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Program output 9