Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*program to find factorial using recursion*/
- #include <stdio.h>
- #include<conio.h>
- int factorial(int n)
- {
- if (n == 0 || n == 1)
- return 1;
- else
- return n * factorial(n - 1);
- }
- int main()
- {
- int num;
- printf("Enter a non-negative integer: ");
- scanf("%d", &num);
- if (num < 0)
- printf("Factorial is not defined for negative numbers.\n");
- else
- {
- int result = factorial(num);
- printf("Factorial of %d is: %d\n", num, result);
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment