Advertisement
iuliaa

factorial

Apr 29th, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int tailrec_cover(int x)
  5. {
  6.     return tailrec_fact(x,1); //apeleaza tailrec_fact cu nr =1
  7. }
  8. int tailrec_fact(int x, int nr)
  9. {
  10.     if(x==0) return nr;
  11.     return tailrec_fact(x-1,x*nr);
  12. }
  13.  
  14. int non_tailrec_fact(int x)
  15. {
  16.     if(x==0) return 1;
  17.     return x*non_tailrec_fact(x-1);
  18. }
  19.  
  20. int directrec_fact(int x)
  21. {
  22.     if(x==0)
  23.         return 1;
  24.     else
  25.         return x*directrec_fact(x-1);
  26. }
  27.  
  28. int decr(int a)
  29. {
  30.     return a-1;
  31. }
  32.  
  33. int indirectrec_fact1(int x)
  34. {
  35.     if(x==0)
  36.         return 1;
  37.     else
  38.         return x*indirectrec_fact2(x-1);
  39. }
  40.  
  41. int indirectrec_fact2(int x)
  42. {
  43.     if(x==0)
  44.         return 1;
  45.     else
  46.         return x*indirectrec_fact1(x-1);
  47. }
  48.  
  49. int main()
  50. {
  51.     printf("%d", indirectrec_fact1(5));
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement