C Programming

Recursive Factorial

C programming for generate factorial with recursive function

5/30/2018
0 views
factorial-recursion.cC
#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
C programmingrecursive functionrecursive factorial numberfactorial numberrecursion

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com