Advertisement
KDOXG

Factorial

Jan 22nd, 2019
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5.     int arg;
  6.     Loop: scanf("%d", &arg);
  7.     printf("%d", fat(arg));
  8.     goto Loop;
  9. }
  10.  
  11. int fat(int n)  //Receives an integer as input and returns it's factorial. If the input is invalid, returns the input itself. Time complexity of O(n)
  12. {
  13.     if (n == 0)
  14.         return 1;
  15.     int i;
  16.     for (i = n - 1; i > 0; i--)
  17.         n = n * i;
  18.     return n;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement