MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / power

Function power

Examples/Modules/Chapter 08/Ex8_02.cpp:17–31  ·  view source on GitHub ↗

Function to calculate x to the power n

Source from the content-addressed store, hash-verified

15
16// Function to calculate x to the power n
17double 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

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected