2

I am trying converts a Fahrenheit degree to Celsius.Now I have a Error,When I use this:

double fahrenheit=input.nextDouble();

double celsius= ( **5** /9)*(fahrenheit-32);(Error)

Then again I am trying given (5.0) then it is work why?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Rehena
  • 25
  • 3

3 Answers3

7

The values '5' and '9' are integers and 5/9 is considered as 0 instead of 0.55 which is why you get the wrong answer. When two integers are divided, the answer is floor() automatically. It is necessary to write either one of '5' and '9' as 5.0 and 9.0

A better practice is to always write both as 5.0 and 9.0 or write them as double(5) and double(9) Since multiplication and other operations are mostly left recursive, this statement would work fine as well. (fahrenheit-32)*5/9; as the statement starts with a double value

Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
3

when you are dividing 5/9 then you will get integer output because both 5 and 9 are integers.But you are dividing 5.0/9 then you are diving double value by integer so the output is double

You can also do (double)5/9

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
2

Then again I am trying given (5.0) then it is work why?

5 / 9 divides integers (whole numbers) and gives you the result 0. 5.0 / 9 divides doubles (numbers that can have a fractional portion), so you get the correct result, 0.5555555555555556.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875