Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This program calculates the average miles per gallon for 3 tanks.
- The average miles per gallon is calculated individually and overall
- for the 3 tanks.
- */
- #include <stdio.h>
- int main()
- {
- //Floating point variables for storing tank values
- float tank1Gals, tank2Gals, tank3Gals, tank1Miles, tank2Miles, tank3Miles, sumGals, sumMiles;
- //Read in information for tank 1
- printf("Enter the number of gallons used for tank #1:\n");
- scanf("%f", &tank1Gals);
- sumGals = tank1Gals; //set initial gallon sum
- printf("Enter the number of miles driven:\n");
- scanf("%f", &tank1Miles);
- sumMiles = tank1Miles; //set initial mileage sum
- printf("*** The miles per gallon for this tank is %0.2f\n", tank1Miles/tank1Gals); //print mpg to two decimal places
- //Read in information for tank 2
- printf("Enter the number of gallons used for tank #2:\n");
- scanf("%f", &tank2Gals);
- sumGals = sumGals + tank2Gals;
- printf("Enter the number of miles driven:\n");
- scanf("%f", &tank2Miles);
- sumMiles = sumMiles + tank2Miles;
- printf("*** The miles per gallon for this tank is %0.2f\n", tank2Miles/tank2Gals); //print mpg to two decimal places
- //Read in information for tank 3
- printf("Enter the number of gallons used for tank #3:\n");
- scanf("%f", &tank3Gals);
- sumGals = sumGals + tank3Gals;
- printf("Enter the number of miles driven:\n");
- scanf("%f", &tank3Miles);
- sumMiles = sumMiles + tank3Miles;
- printf("*** The miles per gallon for this tank is %0.2f\n\n", tank3Miles/tank3Gals); //print mpg to two decimal places
- //Print overall avg mpg by dividing sumMiles by sumGals
- printf("Your overall average miles per gallon for three tanks is %0.2f\n", sumMiles/sumGals);
- printf("Thanks for using the program.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement