You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
690 B

#include <stdio.h> // Include standard input-output library
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0) return 1; // Base case: factorial of 0 is 1
return n * factorial(n - 1); // Recursive call
}
int main() {
int result;
int num;
// Prompt the user to enter an integer
printf("Enter a number: ");
if (scanf("%d", &num) != 1) { // Check if the input is valid
printf("Invalid input.\n");
return 1; // Return 1 to indicate error
}
result = factorial(num); // Calculate factorial
printf("Factorial of %d is %d\n", num, result); // Output the result
return 0; // Return 0 to indicate success
}