Function to calculate x to the power n
| 4 | |
| 5 | // Function to calculate x to the power n |
| 6 | double power(double x, int n) |
| 7 | { |
| 8 | double result{ 1.0 }; |
| 9 | if (n >= 0) |
| 10 | { |
| 11 | for (int i{ 1 }; i <= n; ++i) |
| 12 | result *= x; |
| 13 | } |
| 14 | else // n < 0 |
| 15 | { |
| 16 | for (int i{ 1 }; i <= -n; ++i) |
| 17 | result /= x; |
| 18 | } |
| 19 | return result; |
| 20 | } |
| 21 | |
| 22 | int main() |
| 23 | { |