Write a program to find the factorial of any number

Write a program to find the factorial of any number.
using function 

  1. #include <stdio.h>
  2. int fact(int);
  3.  
  4. void main()
  5. {
  6.  int num, factorial;
  7.  
  8.   printf("Please enter any number to find factorial : ");
  9.   scanf("%d",&num);
  10.   factorial=fact(num);
  11.     printf("The number %d factorial is = %d\n",num,factorial);
  12.     //printf("Factorial of the number (%d) = %d\n",num,fact(num));
  13. }
  14.  
  15. int fact(int n)
  16. {
  17.     int i,f=1;
  18.     for(i=1;i<=n;i++) {
  19.         f=f*i;
  20.     }
  21.     return f;
  22. }

Recursion:

  1. #include <stdio.h>

  2. int fact(int n)
  3. {
  4.     if(n==0)
  5.         return 1;
  6.     else 
  7.         return (n*fact(n-1));
  8. }
  9.  
  10. void main()
  11. {
  12.     int num;
  13.     printf("Please enter any number to find factorial : ");
  14.     scanf("%d", &num);
  15.     printf("The number %d factorial is = %d\n",num,fact(num));
  16. }

একটি মন্তব্য পোস্ট করুন