#include <iostream>
double power (double z, int n)
{
    double result(0.0);
    for (int i = 1; i <= n; i++)
        result *= z;
    return result;
}
int main()
{
    int index(3);
    double x(5.0), double y(0.0);
    y = power (x, index);
    std::cout << y << std::endl;
    return 0;
}
Hello, where is the mistake in this code, please?
Thanks!
Because result is initialised to 0.  And as we know, 0 * anything == 0.  You need to start at 1.
[In future, please learn how to debug!  You would easily have spotted this if you had stepped through your code in a debugger, or added some printf statements to your function.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With