C Program To Find Factorial Of a Number Using Function
In this tutorial, we are going to see how to write a C program to find the factorial of a number using function. The factorial of a number is the product of all integers between 1 and itself. There are four ways to find a factorial of a given number, by using for loop, while loop, recursion, or by creating a function on a range from 1 to X(user entered number). Remember that the end value must be the number entered by the user + 1.
C Program To Find Factorial Of a Number Using Function
#include <stdio.h> long factorial(int n) { int i; long fact = 1; for (i = 1; i <= n; i++) fact = fact * i; return fact; } int main() { int nbr; printf("Enter a number to calculate its factorial: "); scanf("%d", &nbr); printf("%d! = %ld\n", nbr, factorial(nbr)); return 0; }
Output:
Enter a number to calculate its factorial: 3 3! = 6
🚀 Boost your productivity with the best AI tools → Try them