Michael Mkhabela
PRO
last month
Michaelcountry asked

Let's say 1 kilometer is equivalent to 0.621 miles—does that mean we should use int for kilometers and double for miles?

Sudip Bhandari
Expert
last month

Hi Michael! That's a great question!

When converting kilometers to miles, we use the conversion factor:

1 kilometer = 0.621 miles

Since 0.621 is a decimal, there’s a high chance that both the input (kilometers) and the output (miles) could involve decimal values. That’s why it's better to use the double data type for both variables.

In short, use double when you feel the value can include a decimal part, on the other hand, use int when you feel the value cannot include a decimal part (like an age).

I hope this clarifies things for you! Let me know if you have any more questions; I'm here to help.

C++
This question was asked as part of the Learn C++ Basics course.