Factorial using Recursion in C Language

Spread the love
Calculate-Factorial-using-Recursion-in-C-Language

Factorial using recursion in C Program Description:

Write a Program to calculate the Factorial using recursion in C programming language. The program will accept a number from the user and calculate the factorial using recursion (shouldn’t use the loops)

Example Input and Output:

Input:

Enter a Number to calculate factorial : 5

Output:

Factorial of 5 is : 120

Pre-Requisites:

It is recommended to know the basics of the Functions in C language and how recursion works. Please go through the following articles to learn more about them.

Factorial using recursion in C Algorithm/Explanation:

  1. Start the program by accepting a number from the user and storing it in num variable.
  2. Don’t allow negative numbers, So check for negative numbers using num <= 0 condition and display an error message if the user enters a negative number.
  3. Create a function to calculate the factorial recursively named fact function. The fact function takes a number num as input and calculates the factorial of num recursively.
    • The base condition for recursion is num == 1. Whenever the num becomes one, The fact function will return 1.
    • If the num is not zero, Then we will make a recursive call to the fact function and pass the num-1 as the input. fact(num-1)
    • Also, Multiply the result of fact(num-1) with num. – i.e num * fact(num-1)
    • The above process continues until the value of num reaches the 1. (The base condition)
  4. Call the fact function from the main function and pass the num as the input value. The fact function calculates the factorial of num and returns it. Print the result on the console using printf function.

Factorial using Recursion in C Program:

Let’s write the program using above algorithm.

Here we defined the fact function after calling it from the main function, So we have to specify the function declaration ( forward declaration of function).

int fact(int num);

Here are the prototype details of the factfunction.

  • function_name: fact
  • Arguments_list: (int)
  • Return_Value: int
  • Description: The fact function calculates the factorial of a given number recursively and returns the result.

Program Output:

We are using the GCC compiler to compile the program.

📢Related Tutorial: Compile and Run Programs Using GCC

$ gcc factorial-recursive.c

Run the program.

Test Case 1: Positive Numbers:

factorial-of-number-using-recursion-in-c-program-output

Test Case 2: Negative Numbers

As we can see from the above output, The program is providing the valid output and displaying the error message Error: Invalid Input, Try again on a negative number.

Related Recursion Programs:

Related Functions Practice Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Calculate the Factorial of a Number using Recursion […]

Leave a Reply