omri_omri

Factorial C

Nov 24th, 2020
550
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int factor_pointer(int *n);
  4. int factor_recursive(int n);
  5.  
  6. int main(void) {
  7.   int *num;
  8.   int factorial = 0;
  9.  
  10.   printf("Enter a number to factorial:");
  11.   scanf("%d", num);
  12.   factorial  = factor_recursive(*num);
  13.   printf("%d", factorial);
  14.   return 0;
  15. }
  16. //
  17. int factor_pointer(int *n)
  18. {
  19.   int ans =1;
  20.   for(int i =1; i<=*n; i++)
  21.   {
  22.     ans *= i;
  23.   }
  24.   return ans;
  25. }
  26. int factor_recursive(int n)
  27. {
  28.   if(n == 1)
  29.   {
  30.     return 1;
  31.   }
  32.   return n*factor_recursive(n-1);
  33. }
  34.  
  35.  
RAW Paste Data