Function to calculate x to the power n
| 15 | |
| 16 | // Function to calculate x to the power n |
| 17 | double power(double x, int n) |
| 18 | { |
| 19 | double result{ 1.0 }; |
| 20 | if (n >= 0) |
| 21 | { |
| 22 | for (int i{ 1 }; i <= n; ++i) |
| 23 | result *= x; |
| 24 | } |
| 25 | else // n < 0 |
| 26 | { |
| 27 | for (int i{ 1 }; i <= -n; ++i) |
| 28 | result /= x; |
| 29 | } |
| 30 | return result; |
| 31 | } |
| 32 |