Write a C program to find factorial of a given number using recursion.
/* find factorial of the given number using recursion */
int factorial(int num) {
int res = 1;
if (num > 0) {
res= num * factorial(num - 1);
}
return res;
}
int main() {
int num, res;
/* get the input from user */
printf("Enter your input:");
scanf("%d", &num);
/* calculate the factorial of the given number */
res = factorial(num);
/* print the result */
printf("Output:%d\n", res);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input:5
Output:120
Enter your input:5
Output:120
No comments:
Post a Comment