C Examples Factorial
# C Language Examples - Factorial
[ C Language Examples](#)
The factorial of a positive integer (English: factorial) is the product of all positive integers less than or equal to that number, and the factorial of 0 is 1. The factorial of a natural number n is written as n!.
n! = 1 Γ 2 Γ 3 Γ ... Γ n. The factorial can also be defined recursively: 0! = 1, 1! = 1, n! = (n-1)! Γ n.
## Example
#includeint main(){int n, i; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // If the input is negative, display an error if(n<0)printf("Error! Factorial does not exist for negative numbers."); else{for(i=1; i<=n; ++i){factorial *= i; // factorial = factorial*i;}printf("%d! = %llu", n, factorial); }return 0; }
Output:
Enter an integer: 10
10! = 3628800
## Example - Using Recursion
#includelong int multiplyNumbers(int n); int main(){int n; printf("Enter an integer: "); scanf("%d", &n); printf("%d! = %ld", n, multiplyNumbers(n)); return 0; }long int multiplyNumbers(int n){if(n>1)return n*multiplyNumbers(n-1); else return 1; }
[ C Language Examples](#)
[](#)(#)
(#)[](#)
[Volcengine Coding Plan supports Doubao, GLM, DeepSeek, Kimi, MiniMax and other mainstream large models, officially supplied, stable and reliable. Configuration Guide Β₯9.9/month Subscribe Now](https://maas.xfyun.cn/modelSquare?ch=maas_lm_l2E)
YouTip