Advertisement
SkidScripts

Untitled

Nov 29th, 2023
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Function to calculate factorial
  4. unsigned long long calculateFactorial(int number) {
  5.     if (number == 0 || number == 1) {
  6.         return 1;
  7.     } else {
  8.         unsigned long long factorial = 1;
  9.         while (number > 1) {
  10.             factorial *= number;
  11.             number--;
  12.         }
  13.         return factorial;
  14.     }
  15. }
  16.  
  17. int main() {
  18.     int num;
  19.  
  20.     printf("Enter a positive integer: ");
  21.     scanf("%d", &num);
  22.  
  23.     if (num < 0) {
  24.         printf("Factorial is not defined for negative numbers.\n");
  25.     } else {
  26.         unsigned long long result = calculateFactorial(num);
  27.         printf("Factorial of %d = %llu\n", num, result);
  28.     }
  29.  
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement