C Program Find Factorial by Recursion

By | March 9, 2020

C Program Find Factorial by Recursion

#include<stdio.h>
 
// prototype for user defined recursive function
long factorial(int);

// main function
 
int main()
{
  int n;
  long fact;
 
  printf("Enter a number to find its factorial using Recursion=");
  scanf("%d", &n);
 
  if (n < 0)
    printf("Factorial of negative integers isn't defined.\n");
  else
  {
    fact = factorial(n);
    printf("%d! = %ld", n, fact);
  }
 
  return 0;
}

// Function definition for recursive factorial function

long factorial(int n)
{
  if (n == 0) // Base case because factorial of 0 is 1
    return 1;
  else
    return (n*factorial(n-1)); //Recursive, call to itself
}

Output

Enter a number to find its factorial using Recursion=4
4! = 24
——————————–
Process exited after 30.59 seconds with return value 0
Press any key to continue . . .

Loading

4 thoughts on “C Program Find Factorial by Recursion

  1. Luz Gatti

    This website was… how do you say it? Relevant!! Finally I’ve found something that helped me. Thanks a lot!

    Reply
  2. Pingback: Recursion in Java Explained With Examples | EasyCodeBook.com

  3. Pingback: Python Recursive Function Check Palindrom String | EasyCodeBook.com

  4. Pingback: Python Recursion With Example Recursive Function | EasyCodeBook.com

Leave a Reply

Your email address will not be published. Required fields are marked *