C Programming Gas Mileage . Drivers are concerned with the mileage obtained by t
ID: 3696814 • Letter: C
Question
C Programming Gas Mileage .
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a program that will:
• Allow the user to input the miles driven and gallons used for each tankful
• Calculate and display the miles per gallon obtained for each tankful
• Terminate input if the user enters -1 for the gallons used (the program should not ask for miles driven if the number of gallons entered is -1)
• After processing all input information, calculate and print the combined miles per gallon obtained for all tankfuls
• Be sure to check for divide by zero errors
Explanation / Answer
#include<stdio.h>
int main()
{
int mi,gal;
int tmi=0,tgal=0;
while(1)
{
printf("Enter the number of gallons:");
scanf("%d",&gal);
if(gal==-1)break;
if(gal==0)
{
printf("Number of gallons cannot be zero ");
continue;
}
printf("Enter the number of miles:");
scanf("%d",&mi);
printf("The number of miles per gallon is %f ",mi*1.0/gal);
tmi+=mi;
tgal+=gal;
}
printf("The total number of miles per gallon is %f ",tmi*1.0/tgal);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.