Read the number of miles traveled and the number of gallons of gasoline consumed and then calculate and display the miles-per-gallon value - C Operator

C examples for Operator:Arithmetic Operator

Introduction

Showing one place to the right of the decimal.

One gallon is about 3.785 liters and one mile is about 1.609 kilometers, convert the mile- per-gallon value to a liters-per-100-km value.

Demo Code

#include <stdio.h>

int main(void)
{
  const float KM_PER_MILE = 1.609;
  const float LT_PER_GALLON = 3.785;

  float miles_travelled, gallons_gas_consumed;
  float miles_per_gallon, liters_per_100km;

  printf("distance in miles: ");
  scanf("%f", &miles_travelled);

  printf("Enter the amount of gas consumed in gallons: ");
  scanf("%f", &gallons_gas_consumed);

  // calculate miles per gallon and liters per km
  miles_per_gallon = miles_travelled / gallons_gas_consumed;
  liters_per_100km = 100. / miles_per_gallon * LT_PER_GALLON / KM_PER_MILE;

  printf("Miles per gallon: %.1f\n", miles_per_gallon);
  printf("Liters per 100 kilometers: %.1f\n", liters_per_100km);

  return 0;//from   w ww.j av a  2  s  .com
}

Result


Related Tutorials