Advertisement
Guest User

Steve Bradford

a guest
Oct 12th, 2009
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. /* This program calculates the average miles per gallon for 3 tanks.  
  2.    The average miles per gallon is calculated individually and overall
  3.    for the 3 tanks.
  4. */
  5.  
  6.  
  7. #include <stdio.h>
  8.  
  9.  
  10.  
  11. int main()
  12. {
  13.    
  14.     //Floating point variables for storing tank values
  15.     float tank1Gals, tank2Gals, tank3Gals, tank1Miles, tank2Miles, tank3Miles, sumGals, sumMiles;
  16.  
  17.     //Read in information for tank 1
  18.     printf("Enter the number of gallons used for tank #1:\n");
  19.     scanf("%f", &tank1Gals);   
  20.  
  21.     sumGals = tank1Gals;  //set initial gallon sum
  22.  
  23.     printf("Enter the number of miles driven:\n");
  24.     scanf("%f", &tank1Miles);
  25.  
  26.     sumMiles = tank1Miles;  //set initial mileage sum
  27.  
  28.     printf("*** The miles per gallon for this tank is %0.2f\n", tank1Miles/tank1Gals);  //print mpg to two decimal places
  29.  
  30.     //Read in information for tank 2
  31.     printf("Enter the number of gallons used for tank #2:\n");
  32.     scanf("%f", &tank2Gals);   
  33.  
  34.     sumGals = sumGals + tank2Gals;
  35.  
  36.     printf("Enter the number of miles driven:\n");
  37.     scanf("%f", &tank2Miles);  
  38.  
  39.     sumMiles = sumMiles + tank2Miles;
  40.  
  41.     printf("*** The miles per gallon for this tank is %0.2f\n", tank2Miles/tank2Gals); //print mpg to two decimal places
  42.  
  43.     //Read in information for tank 3
  44.     printf("Enter the number of gallons used for tank #3:\n");
  45.     scanf("%f", &tank3Gals);   
  46.  
  47.     sumGals = sumGals + tank3Gals;
  48.  
  49.     printf("Enter the number of miles driven:\n");
  50.     scanf("%f", &tank3Miles);  
  51.  
  52.     sumMiles = sumMiles + tank3Miles;
  53.    
  54.     printf("*** The miles per gallon for this tank is %0.2f\n\n", tank3Miles/tank3Gals); //print mpg to two decimal places
  55.  
  56.     //Print overall avg mpg by dividing sumMiles by sumGals
  57.     printf("Your overall average miles per gallon for three tanks is %0.2f\n", sumMiles/sumGals);
  58.  
  59.     printf("Thanks for using the program.\n");
  60.    
  61.  
  62.     return 0;
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement