Get Website Learn Coding Tech Story

C Programming Examples

#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

Learn more about Factorial