Hello starterpack6100,
You are welcome.
What you are trying to accomplish in a program will determine what type of variable to use.
Integers only work with whole numbers where as "double"s and "float"s, "float"s are less precise than "doubles" and not used as much these days, work with floating point numbers.
What you need for a calculation or for output helps determine the type of variable. In this case when you put a floating point number into an int you only keep the whole number and loose the decimal part. Also dividing by (9 / 5) as an integer results in "1" as
ne555
has pointed out. By dividing (9.0 / 5.0) the result is "1.8" which will make a big difference in your calculation.
By defining "c" and "f" as a "double" it is a way to avoid any data loss when storing the result of a calculation (what would be a double) into an "int".
To save time of having to type case everything as a "double" you start by defining the variables as "double"s and use "9.0 and 5.0" in the calculation. Technically speaking only one of those numbers needs to be a "double" and the other will be promoted to a "double" for the calculation.
Another way to think of this is that anything on the rhs of the "=" needs to be a "double" or used in a way that the compiler will promote the variable to a "double".
BTW while I am thinking about it using "c" and "f" for variable names is not a problem with code this short, but with a larger amount of code that could span two or more screens variable names of "celsius" and "ferenheit"make the code easier to follow. Also you should avoid single letter variable names.
As
Repeater once wrote:
You know you're not being charged for every letter you use, right? If you want people to be able to read your code (which you do), use variable names that help people understand what you're trying to do.
|
Hope that helps,
Andy