> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c05/ft_recursive_factorial.md).

# ft\_recursive\_factorial

**Objective:**

Create a **recursive function** that calculates the factorial of a given number. The function should return **0** if the input is invalid (i.e., negative numbers).

**Turn-in Directory:**

ex01/

**Files to Turn In:**

ft\_recursive\_factorial.c

**Allowed Functions:**

None

**Prototype:**

```c
int ft_recursive_factorial(int nb);
```

***

#### **Detailed Explanation**

The **ft\_recursive\_factorial** function calculates the factorial of a given number `nb` using recursion. The factorial of a number `n` is defined as the product of all positive integers less than or equal to `n`. For example:

* Factorial of 5: `5! = 5 * 4 * 3 * 2 * 1 = 120`
* Factorial of 0: `0! = 1`

The function returns:

* The factorial of `nb` if `nb` is a non-negative integer.
* **0** if `nb` is negative (invalid input).

***

#### **Code Implementation**

```c
int ft_recursive_factorial(int nb)
{
    if (nb < 0)  // Handle invalid input (negative numbers)
        return (0);
    if (nb <= 1)  // Base case: factorial of 0 or 1 is 1
        return (1);
    return (nb * ft_recursive_factorial(nb - 1));  // Recursive case
}
```

***

#### **Example Usage**

```c
#include <stdio.h>

int ft_recursive_factorial(int nb);

int main(void)
{
    printf("Factorial of 5: %d\n", ft_recursive_factorial(5));  // Output: 120
    printf("Factorial of 0: %d\n", ft_recursive_factorial(0));  // Output: 1
    printf("Factorial of -3: %d\n", ft_recursive_factorial(-3)); // Output: 0
    printf("Factorial of 10: %d\n", ft_recursive_factorial(10)); // Output: 3628800

    return 0;
}
```

***

#### **Edge Cases to Consider**

1. **Negative Numbers**:
   * Input: `-3` → Returns **0** (invalid input).
2. **Zero**:
   * Input: `0` → Returns **1** (by definition, `0! = 1`).
3. **Positive Numbers**:
   * Input: `5` → Returns **120** (`5! = 120`).
4. **Large Numbers**:
   * Input: `10` → Returns **3628800** (`10! = 3628800`).

***

#### **Limitations of ft\_recursive\_factorial**

1. **No Handling of Overflows**:
   * The function does not handle integer overflows. For large values of `nb`, the result may exceed the range of an `int`, leading to undefined behavior.
2. **Recursion Depth**:
   * For very large values of `nb`, the function may cause a stack overflow due to excessive recursion depth.

***

#### **How It Works**

1. **Base Case 1 (Invalid Input)**:

   * If `nb` is negative, the function immediately returns **0**.

   ```c
   if (nb < 0)
       return (0);
   ```
2. **Base Case 2 (Factorial of 0 or 1)**:

   * If `nb` is **0** or **1**, the function returns **1** because the factorial of 0 and 1 is 1.

   ```c
   if (nb <= 1)
       return (1);
   ```
3. **Recursive Case**:

   * For any positive integer greater than 1, the function calls itself with `nb - 1` and multiplies the result by `nb`.

   ```c
   return (nb * ft_recursive_factorial(nb - 1));
   ```

***

#### **Key Points to Remember**

1. **Recursion**:
   * The function uses recursion to calculate the factorial, which means it calls itself with a smaller value of `nb` until it reaches the base case.
2. **Base Cases**:
   * The base cases ensure the recursion terminates correctly and handle edge cases like `0` and negative numbers.
3. **Efficiency**:
   * The time complexity is **O(n)**, where `n` is the value of `nb`. Each recursive call reduces `nb` by 1 until it reaches the base case.

***

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c05/ft_recursive_factorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
