Program to calculate factorial of a number using recursion in C

Source Code

// 34. Program to calculate factorial of a number using recursion
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>
//#include <stdlib.h>

int factorial(int);

int main () {
	int num, fact=1;
	//clrscr();

	printf("\t\t..:: calculate factorial of a number using recursion ::..\n\n");
	printf("please enter any number: ");
	scanf("%d", &num);

	fact = factorial(num);
	printf("\nFactorial of %d is %d", num, fact);

	//getch();
}

int factorial(int num) {
	if (num == 0) {
		return 1;
	} else {
		return num * factorial(num-1);
	}
}

/*int factorial(int num, int fact) {
	if (fact == 1 && num == 0)
		fact = 0;

	if (num > 0) {
		fact *= num;
		num--;
		factorial(num, fact);
	} else {
		return fact;
	}
}*/

Output

		..:: calculate factorial of a number using recursion ::..

please enter any number: 5

Factorial of 5 is 120

Leave a Reply

Note: Your email address will not be published. Required fields are marked *