How to write a C Program Factorial in C Programming Language Example ?
Solution:
- #include <stdio.h>
- int fac1(int n, int acc){
- return (n > 0 ) ? fac1(n-1, n*acc) : acc ;
- }
- int fac(int n){
- int acc = 1;
- return fac1(n, acc);
- }
- int fac2(int n){
- int acc = 1;
- for(int i = 0; i < n; i++)
- acc *= (n-i);
- return acc;
- }
- int main(int argc, char *argv[]) {
- int num;
- scanf("%d", &num);
- printf("%d\n", fac(num));
- printf("%d\n", fac2(num));
- return 0;
- }