Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C language. So, please write the code with C language. Thanks for your h

ID: 3878654 • Letter: T

Question

This is C language. So, please write the code with C language. Thanks for your help!

Program 2 A car's miles-per-gallon (MPG) can be calculated with the following formula: Design a function called mpg that can be run from your chapter 2 driver program. It should have the following function header: The program should ask the user for the number of miles driven and the gallons of gas used. MPG = Miles driven / Gallons of gas used void mpg(void) Select appropriate data types for each variable Calculate the car's miles-per- gallon and display the results. Test several times before submitting! Only attach your C program file for this function.

Explanation / Answer

/*Calculate car's miles per gallon */

#include<stdio.h>

#define mpg main

/* Before program is given to compiler it will be processed by preprocessor and it will scenned from to bottom.
mpg will replace with main,this way we can achieve to write a c program without main function but internally
we are using main function. */

void mpg(void)

{

/* void data typeused here to state that we are not taking any parameter and

not returning anything from the function.

This function prompts to the user for asking values for miles and gallons and display mpg.

*/

float miles,gallons,miles_per_gallon;

printf(" Enter the number of gallons used:");//Ask user for enter gallons used

scanf("%f",&gallons);//store the gallons value in gallons variable

printf(" Enter the miles of driven:");//Ask user for enter miles driven by car

scanf("%f",&miles);//store the miles value in miles variable

miles_per_gallon = miles/gallons;//calculate miles per gallon and store into the miles_per_gallon variable

printf(" The miles per gallon: %f",miles_per_gallon);//Display miles per gallon to the user

return;//return from the function after complete te calculation.

}

/*Here in above function calculated mpg for one time tank full but if you want to calculate it more than one time

without exiting program.We can loop it and calculate again and again multiple times till the user want to exit.

You can call this function 'mpg();' from main function.*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote