{ }codelper
float divisionP0216

Distance per liter

A car drove d kilometers and used l liters of fuel. One line gives the integers d and l separated by one space. Print the distance per liter of fuel, that is d ÷ l, with exactly two digits after the decimal point. Write two digits even when the quotient is a whole number (25 ÷ 5 → 5.00). The input always has d × 100 divisible by l.
So the quotient ends within two decimal places, and no rounding is ever needed.

Input
1 ≤ d ≤ 100,000, 1 ≤ l ≤ 1,000, d × 100 is divisible by l
Output
d ÷ l on one line, exactly two digits after the decimal point
Example 1 · stdin
25 2
stdout
12.50
Example 2 · stdin
3 4
stdout
0.75
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

The trap is integer division.
In C, C++, Java and C#, d / l divides two ints and keeps only the quotient (3 / 4 → 0).
Turn one side into a floating-point number first: (double)d / l in C, Java and C#, static_cast<double>(d) / l in C++.
Converting after dividing, as in (double)(d / l), or just storing into a double, as in double km = d / l;, comes too late: the fraction is already gone and 3 4 gives 0.00.
In Python 3, JS, TS and Craykon, / divides integers with decimals (Python's // keeps only the quotient).
The second trap is the number of digits.
Printing plainly gives 12.5 for Python print(d / l) and JS/TS console.log(d / l), and for a whole quotient JS prints 5 and Python 5.0.
Fix two decimals with Python f"{x:.2f}", JS/TS x.toFixed(2), C printf("%.2f"), C++ std::fixed << std::setprecision(2), Java String.format("%.2f", x), C# x.ToString("F2") and Craykon Str.Fixed(x, 2).

Pythonmain.py · 3 lines
d, l = map(int, input().split())# Python 3의 /는 정수끼리도 소수까지 나눈다(//는 몫만)print(f"{d / l:.2f}")
JavaScriptmain.js · 3 lines
const [d, l] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// toFixed(2)가 소수 두 자리 문자열을 만든다(12.5 → "12.50")console.log((d / l).toFixed(2));
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const [d, l]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log((d / l).toFixed(2));
JavaMain.java · 13 lines
import java.util.Locale;import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int d = sc.nextInt();        int l = sc.nextInt();        // 한쪽을 double로 바꿔야 실수 나눗셈이 된다        double km = (double) d / l;        System.out.println(String.format(Locale.ROOT, "%.2f", km));    }}
C#Main.cs · 15 lines
using System;using System.Globalization; class Program{    static void Main()    {        string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int d = int.Parse(p[0]);        int l = int.Parse(p[1]);        // int끼리 나누면 몫만 남으니 한쪽을 double로        double km = (double)d / l;        Console.WriteLine(km.ToString("F2", CultureInfo.InvariantCulture));    }}
C++main.cpp · 9 lines
#include <iomanip>#include <iostream> int main() {    int d, l;    std::cin >> d >> l;    double km = static_cast<double>(d) / l;    std::cout << std::fixed << std::setprecision(2) << km << "\n";}
Cmain.c · 10 lines
#include <stdio.h> int main(void) {    int d, l;    scanf("%d %d", &d, &l);    /* int끼리 나누면 소수가 버려진다. (double)로 한쪽을 바꾼다 */    double km = (double)d / l;    printf("%.2f\n", km);    return 0;}
Craykonmain.cray · 5 lines
p = Console.ReadLine().Trim().Split(" ");d = p[0].ToNumber();l = p[1].ToNumber();// 크레이콘의 /는 늘 소수까지 나눈다Console.WriteLine(Str.Fixed(d / l, 2));