# Problems calculating distance and mpg

Printable View

• 09-09-2010
levitylek
Problems calculating distance and mpg
I'm supposed to build a program that calculates miles per gallon when given the radius & revolutions of a car's tires, and the amount of gas used, but every time I try to test it out the final answer is always slightly off from the correct answer.

Code:

```#include <stdio.h> //Constants const int PI = 3.14159; #define INCHES_IN_MILE 63360 int main() {                 int revs;         double rad, gas, dist;                 //Get user input.         printf("What is the radius of your tires, in inches?\n");         scanf("%lf", &rad);                 printf("How many revolutions did your car's tires make?\n");         scanf("%d", &revs);                 printf("How many gallons of gas did your car use?\n");         scanf("%lf", &gas);                 //Calculate & print MPG         dist = ((rad*2*PI)*(revs)) / INCHES_IN_MILE;         printf("Your car averaged %.2f miles per gallon.\n", dist/gas);         return 0; }```
Did I mess up the code for the math somewhere?
The correct math should be the radius x 2 x Pi, which is the circumference, and then that multiplied by the tire revolutions gives you the distance in inches. Divide that by the amount of inches in a mile gives you the distance traveled in miles. And when the distance is divided by the amount of gas used it should give you the mpg.

An example of an answer I get is:

Radius: 15
Revolutions: 10,000
Gallons: 0.75

The mpg should be 19.83 but the program gives me 18.94.

Any ideas?
• 09-09-2010
msh
Right here.

Code:

`const int PI = 3.14159;`
• 09-09-2010
Adak
Welcome to the forum, Levitylek.

That int data type had me rolling! :D :D
• 09-09-2010
levitylek
Gah thank you so much!
In our instructions it said something about making a Pi a constant and that tripped me up but now that I go back and read it says define a constant for Pi :rolleyes:
• 09-09-2010
Adak
msh gets the Eagle-eye award, methinks! Good catch! :)
• 09-09-2010
CommonTater
Why would you use #define for inches in a mile but not for Pi ?