> 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_iterative_power.md).

# ft\_iterative\_power

**Objective:**

Create an **iterative function** that calculates the power of a given number `nb` raised to the exponent `power`. The function should return **0** if the exponent is negative and **1** if the exponent is **0**.

**Turn-in Directory:**

ex02/

**Files to Turn In:**

ft\_iterative\_power.c

**Allowed Functions:**

None

**Prototype:**

```c
int ft_iterative_power(int nb, int power);
```

***

#### **Detailed Explanation**

The **ft\_iterative\_power** function calculates the value of `nb` raised to the power of `power` using iteration. For example:

* `2^3 = 2 * 2 * 2 = 8`
* `5^0 = 1` (any number raised to the power of 0 is 1)
* `3^-2 = 0` (negative exponents are not handled, so the function returns 0)

The function returns:

* The result of `nb^power` if `power` is a non-negative integer.
* **1** if `power` is **0**.
* **0** if `power` is negative.

***

#### **Code Implementation**

```c
int ft_iterative_power(int nb, int power)
{
    int result;

    result = nb;
    if (power == 0)  // Any number raised to the power of 0 is 1
        return (1);
    else if (power < 0)  // Negative exponents are not handled
        return (0);
    while (power > 1)  // Iterative calculation of power
    {
        result *= nb;
        power--;
    }
    return (result);
}
```

***

#### **Example Usage**

```c
#include <stdio.h>

int ft_iterative_power(int nb, int power);

int main(void)
{
    printf("2^3: %d\n", ft_iterative_power(2, 3));  // Output: 8
    printf("5^0: %d\n", ft_iterative_power(5, 0));  // Output: 1
    printf("3^-2: %d\n", ft_iterative_power(3, -2)); // Output: 0
    printf("10^5: %d\n", ft_iterative_power(10, 5)); // Output: 100000

    return 0;
}
```

***

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

1. **Power of 0**:
   * Input: `5^0` → Returns **1** (any number raised to the power of 0 is 1).
2. **Negative Power**:
   * Input: `3^-2` → Returns **0** (negative exponents are not handled).
3. **Positive Power**:
   * Input: `2^3` → Returns **8** (`2 * 2 * 2 = 8`).
4. **Large Power**:
   * Input: `10^5` → Returns **100000** (`10 * 10 * 10 * 10 * 10 = 100000`).

***

#### **Limitations of ft\_iterative\_power**

1. **No Handling of Negative Exponents**:
   * The function does not handle negative exponents and returns **0** for such cases.
2. **No Handling of Overflows**:
   * The function does not handle integer overflows. For large values of `nb` and `power`, the result may exceed the range of an `int`, leading to undefined behavior.

***

#### **How It Works**

1. **Initialization**:

   * The variable `result` is initialized to `nb` to store the intermediate result of the power calculation.

   ```c
   result = nb;
   ```
2. **Power of 0**:

   * If `power` is **0**, the function immediately returns **1** (since any number raised to the power of 0 is 1).

   ```c
   if (power == 0)
       return (1);
   ```
3. **Negative Power**:

   * If `power` is negative, the function returns **0** (negative exponents are not handled).

   ```c
   else if (power < 0)
       return (0);
   ```
4. **Iterative Calculation**:

   * The `while` loop multiplies `result` by `nb` and decrements `power` until `power` becomes **1**.

   ```c
   while (power > 1)
   {
       result *= nb;
       power--;
   }
   ```
5. **Return the Result**:

   * Once the loop ends, the function returns the value of `result`, which is `nb^power`.

   ```c
   return (result);
   ```

***

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

1. **Iterative Approach**:
   * The function uses a `while` loop to calculate the power, avoiding recursion.
2. **Edge Cases**:
   * The function handles edge cases like `power = 0` and `power < 0` explicitly.
3. **Efficiency**:
   * The time complexity is **O(power)**, where `power` is the exponent. Each iteration reduces `power` by 1 until it reaches **1**.

***

#### **Best Practices**

1. **Input Validation**:
   * Always check for invalid inputs (e.g., negative exponents) to avoid unexpected behavior.
2. **Edge Cases**:
   * Test edge cases like `power = 0`, `power < 0`, and large values of `power` to ensure the function behaves as expected.
3. **Overflow Handling**:
   * Consider adding overflow handling for large inputs, though it is not required in this exercise.


---

# 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_iterative_power.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.
