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

Function power

Examples/Modules/Chapter 08/Ex8_01.cpp:6–20  ·  view source on GitHub ↗

Function to calculate x to the power n

Source from the content-addressed store, hash-verified

4
5// Function to calculate x to the power n
6double 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
22int main()
23{

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected