Write a function that calculates the factorial of a number using recursion.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>

double fact(int nb)
{
	if (nb == 0)
		return 1;
	return nb * fact(nb - 1);
}

void main()
{
	int nb;

	printf("Enter an integer: ");
	scanf("%d", &nb);

	printf("%d!=%.0lf", nb, fact(nb));

	getch();
}

 

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Reverse level order