Search code examples
cexponent

Having trouble with pow() in C


I am currently reading an online version of Stephen Kochan's "Programming in C (3rd Edition)." One of the activities involves evaluating an equation,

Write a program that evaluates the following expression and displays the results (remember to use exponential format to display the result): (3.31 x 10-8 x 2.01 x 10-7) / (7.16 x 10-6 + 2.01 x 10-8)

When I attempt to do this, the output is always 0.0000. Here is my code.

#include <stdio.h>
int main (void) {
float result;
    result = (3.31 * pow(10,-8) * 2.01 * pow(10,-7)) / (7.16 * pow(10,-6) + 2.01 * pow(10, -8));
    printf ("%f", result);
    return 0;
}

If I am doing anything wrong, please point it out. If you have any tips, please say so.


Solution

  • You must #include <math.h>

    Also, change to this:

    printf ("%e\n", result);
    

    You should probably also have

    double result;
    

    because pow() returns a double.