Reputation: 13
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.
Upvotes: 0
Views: 609
Reputation: 1
Use %e to get an exponent result value as instructed.
int main (void)
{
float i;
float j;
float result;
result = i / j;
i = 3.31 * 10 -8 * 2.01 * 10 -7;
j = 7.16 * 10 -6 + 2.01 * 10 -8;
printf("%e", i, j, result);
return 0;
}
Upvotes: -1
Reputation: 2678
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
.
Upvotes: 5