Deitel & Deitel, C How to Program 3rd Edition, Exercise 3.17

/*Because of the high price of gasoline, drivers are concerned with the mileage obtained
by their automobiles. One driver has kept track of several tankfuls of gasoline bu recording
miles driven and gallons used for each tankful. Develop a C program that will input the miles
driven and gallons used for each tankful. The program should calculate and display the miles
per gallon  obtained for each tankful. After processing all input information, the program
should calculate and print the combined miles per gallon obtained for all tankfuls*/

#include <stdio.h>

int main() {
int miles, i = 0;
float gallon = 0, average, total = 0;
while (gallon != -1) {
printf("Enter the gallons used(-1 to end): ");
scanf("%f", &gallon);
if (gallon != -1) {
printf("Enter the miles driven: ");
scanf("%d", &miles);
printf("The miles/gallon for this tank was %lf\n\n", miles / gallon);
total += miles / gallon;
i++;
}
}
average = total / i;
printf("\nThe overall average miles/gallon was %lf\n\n", average);


return 0;
}


Let me know in the comments if this page helped you somehow.

It is nice to know if my work helped someone, thanks :)

Comments

Popular posts from this blog

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.23 (Turtle Graphics)

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.22 (Total Sales)

Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)