Factorial in C


SUBMITTED BY: kaushal1981

DATE: Feb. 17, 2017, 1:45 a.m.

FORMAT: Text only

SIZE: 519 Bytes

HITS: 945

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n, i;
  5. unsigned long long factorial = 1;
  6. printf("Enter an integer: ");
  7. scanf("%d",&n);
  8. // show error if the user enters a negative integer
  9. if (n < 0)
  10. printf("Error! Factorial of a negative number doesn't exist.");
  11. else
  12. {
  13. for(i=1; i<=n; ++i)
  14. {
  15. factorial *= i; // factorial = factorial*i;
  16. }
  17. printf("Factorial of %d = %llu", n, factorial);
  18. }
  19. return 0;
  20. }

comments powered by Disqus