-4

I have following code

int num1 = 8;
int num2 = 5;

double ans = num1/num2;

System.out.println(ans); // result is 1.0

why i am getting 1.0 in ans where it should be 1.6 ?

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33

1 Answers1

5

No, it not should be 1.6

Here how it is complied

int / int -> result int.

i.e. 8/5 = 1

then int value assigned to double.

int -> double // is 1.0

if you use this :

double ans =(double) num1/num2;

this will give 1.6 because num1 will be considered as double and

double / int -> result double.
Saif
  • 6,804
  • 8
  • 40
  • 61