AbdulFathaah

factorial using recursion

Nov 13th, 2023 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. /*program to find factorial using recursion*/
  2.  
  3. #include <stdio.h>
  4. #include<conio.h>
  5. int factorial(int n)
  6. {
  7. if (n == 0 || n == 1)
  8. return 1;
  9. else
  10. return n * factorial(n - 1);
  11. }
  12.  
  13. int main()
  14. {
  15. int num;
  16. printf("Enter a non-negative integer: ");
  17. scanf("%d", &num);
  18. if (num < 0)
  19. printf("Factorial is not defined for negative numbers.\n");
  20. else
  21. {
  22. int result = factorial(num);
  23. printf("Factorial of %d is: %d\n", num, result);
  24. }
  25. getch();
  26. return 0;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment