Answer:
The C program is given below.
#include<stdio.h>
int main() {
double speed, gas_cost, cost_1, cost_10, cost_50, cost_400;
printf("Enter car's speed in miles/gallon ");
scanf("%lf", &speed);
printf("Enter cost of gas in dollars/gallon ");
scanf("%lf", &gas_cost);
// fraction gives the gallons of gas needed for 1 mile.
// gallons of gas needed for 1 mile * cost of 1 gallon of gas = gas cost for 1 mile
cost_1 = (1/speed)*gas_cost ;
// cost of gas for 10 miles
cost_10 = cost_1 * 10;
printf("Cost of gas for 10 miles in dollars is ");
printf( "%0.2f", cost_10 );
printf("\\");
// cost of gas for 50 miles
cost_50 = cost_1 * 50;
printf("Cost of gas for 50 in dollars miles is ");
printf( "%0.2f", cost_50 );
printf("\\");
// cost of gas for 400 miles
cost_400 = cost_1 * 400;
printf("Cost of gas for 400 in dollars miles is ");
printf( "%0.2f", cost_400 );
printf("\\");
return 0;
}
OUTPUT
Enter car's speed in miles/gallon 20
Enter cost of gas in dollars/gallon 30
Cost of gas for 10 miles in dollars is 15.00
Cost of gas for 50 in dollars miles is 75.00
Cost of gas for 400 in dollars miles is 600.00
Step-by-step explanation:
This program takes user input for cost of gas in dollars and speed of the car in miles per gallon.
Next, gallons needed for 1 mile is calculated.
cost_1 = (1/speed)*gas_cost ;
Following this, gallons needed for 10 miles, 50 miles and 400 miles are calculated.
cost_10 = cost_1 * 10;
cost_50 = cost_1 * 50;
cost_400 = cost_1 * 400;
These values are displayed on the screen with two decimal places by using printf( "%0.2f", your_value).
All the variables needed in this program are declared as double.
All the instructions mentioned have been implemented. This program can be tested for all possible positive numbers.