-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2-2.c
More file actions
30 lines (23 loc) · 702 Bytes
/
Copy pathlab2-2.c
File metadata and controls
30 lines (23 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
/* Function declaration for recursive factorial */
int factorial_recursive(int n);
int main() {
int number;
int result;
/* Get the number from the user */
printf("Enter a positive integer: ");
scanf("%d", &number);
/* Calculate the factorial using recursive function */
result = factorial_recursive(number);
/* Output the result */
printf("Factorial of %d is: %d (Recursive)\n", number, result);
return 0;
}
/* Recursive factorial function */
int factorial_recursive(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: 0! = 1! = 1
} else {
return n * factorial_recursive(n - 1); // Recursive case
}
}