Program To Calculate Factorial of a Number using Function - C Program

Program To Calculate Factorial of a Number using Function

In mathematics,the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5 ! = 5 × 4 × 3 × 2 × 1 = 120.

#include <stdio.h>
int factorial(int num);
int main()
{
	int num,fn;
	printf("Enter the number :");
	scanf("%d",&num);
	fn=factorial(num);
	printf("Factorial Number of %d is %d",num,fn);
	printf("\nEnter another number :");
	scanf("%d",&num);
	fn=factorial(num);
	printf("Factorial Number of %d is %d",num,fn);
    return 0;
}
int factorial(int num)
{
	int fn=1;
    while(num>=1)
    {
		fn=fn*num;
		num--;
    }
	return fn;
}

Output - Program To Calculate Factorial of a Number Using Function

Enter the number :4    
Factorial Number of 4 is 24
Enter another number :6    
Factorial Number of 6 is 720
Compile and Run Online