Recursive Factorial

#include<stdio.h>

int factorial(int);
int main() {
	long int no, f = 1;
	printf("Calculate Factorial\n");
	printf("Enter a number:\n");
	scanf("%ld", &no);

	/* If the given number is not zero then factorial calculation start */ 
	if(no != 0) {

		/* Call factorial function by passing integer value and receive result in 'f' variable */
		f = factorial(no);
		printf("Factorial: %ld\n", f);
	}

	/* If the given number is zero then factorial will be one */
	else
		printf("Factorial: 1\n");
	return 0;
}


/* Recursive function */
int factorial(int no) {
	if(no == 1)
		return 1;
	else
		/* Call the factorial function inside in it */
		return(no * factorial(no - 1));
}



/* Output */
Calculate Factorial
Enter a number:
5

Factorial: 120

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!