Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Division int and double | Introduction
C++ Data Types

book
Division int and double

We will not stop on most operations since they work as expected. Instead, let's look at the reasons behind the incorrect result obtained from the division operation. Those are explained in a video below:

It's not just a problem limited to division. When you use operators such as +, -, *, or / on variables of certain types, the resulting value will have the same type as the operands.

It's important to be mindful of these possibilities and take them into account.

Opgave

Swipe to start coding

  1. Calculate and output how many meters in a given number of kilometers (2455).
  2. Make sure the output number is float and not integer.

Note

In order to get the correct result, we only need to add . to any of the numbers in division (9/4. or 9./4 or 9./4.).

Løsning

solution.cpp

solution.cpp

#include <iostream>

int main()
{
// Make sure to add . to one of the numbers to get the correct result
std::cout << 2455. / 1000 << std::endl;
}

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

#include <iostream>

int main()
{
// Make sure to add . to one of the numbers to get the correct result
std::cout << 2455 / 1000 << std::endl;
}

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt