Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

# How to Write a C Program to Calculate a Bike's Average Consumption

write a simple C program that can calculate a bike's average consumption from the given total distance traveled (in km) and spent fuel (in liters).

If you are a bike enthusiast, you might be interested in knowing how much fuel your bike consumes on average. This can help you plan your trips, budget your expenses, and compare different models of bikes. In this program, we will show you how to write a simple C program that can calculate a bike's average consumption from the given total distance traveled (in km) and spent fuel (in liters).

#### What is Average Consumption?

Average consumption is a measure of how efficiently a vehicle uses fuel. It is usually expressed in kilometers per liter (km/l), which means how many kilometers a vehicle can travel with one liter of fuel. The higher the average consumption, the more fuel-efficient the vehicle is.

#### How to Calculate Average Consumption?

The formula for calculating average consumption is:

Average consumption = Total distance / Spent fuel

For example, if a bike travels 350 km and uses 5 liters of fuel, its average consumption is: Average consumption = 350 / 5 = 70 km/l. This means that the bike can travel 70 km with one liter of fuel.

#### C Program

To write a C program that can calculate a bike's average consumption, we need to follow these steps:

1. Declare three variables: an integer variable for the total distance, a float variable for the spent fuel, and another float variable for the average consumption.
2. Prompt the user to enter the total distance in km and store it in the integer variable using scanf() function.
3. Prompt the user to enter the spent fuel in liters and store it in the float variable using scanf() function.
4. Calculate the average consumption using the formula and store it in the float variable.
5. Print the result with two decimal points using printf() function.

Here is the complete code for the C program:

#include <stdio.h>

int main()

{

int distance; // total distance travelled in km

float fuel; // spent fuel in liters

float average; // average consumption in km/l

printf("Enter the total distance travelled in km: ");

scanf("%d", &distance);

printf("Enter the spent fuel in liters: ");

scanf("%f", &fuel);

average = distance / fuel; // calculate the average consumption

printf("The average consumption is %.2f km/l\n", average); // print the result with 2 decimal points

return 0;

}

#### Output

This will prompt us to enter the total distance and spent fuel, and then display the average consumption. For example:

Enter the total distance travelled in km: 350

Enter the spent fuel in liters: 5

The average consumption is 70.00 km/l

We can test the program with different inputs and see if it gives us correct outputs.

#### Conclusion

In this program, we have learned how to write a C program that can calculate a bike's average consumption from the given total distance traveled (in km) and spent fuel (in liters). We hope that this tutorial was helpful and informative for you. If you have any questions or feedback, please feel free to leave a comment below.

We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!