0

I was trying to solve a simple mathematical problem with java but stumbles with this problem:

public static void main(String[] arg)
{
    double result = 33/2;
    System.out.println(result);
}

Output was 16.0 but after checking it with a calculator the result should be 16.5 I am really confuse and want to know what is happening

  • Do it as `33/2.0` or `33.0/2` or `(double)33/2` or `33/(double)2` – Arvind Kumar Avinash Mar 12 '21 at 09:35
  • 1
    When you divide *integers* like `33 / 2` the result will be *integer* as well; `16` your case (`33 % 2` returns remainder which is `1`). If you want to have a *floating point* outcome you should have at least one number being floating point value: `33.0 / 2`, `33 / 2.0` or `33.0 /2.0` – Dmitry Bychenko Mar 12 '21 at 09:38

0 Answers0