Why did i get 0 here as the output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why did i get 0 here as the output

public class Program { public static void main(String[] args){ System.out.println(9/10); } }

6th Jun 2018, 4:59 PM
stephen haokip
stephen haokip - avatar
6 Answers
+ 4
Because 10 goes into 9 zero times. So the answer is 0.
6th Jun 2018, 5:02 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
System.out.println(9.0/10)... Basically make one or both the inputs double data type
6th Jun 2018, 5:13 PM
cyk
cyk - avatar
+ 1
how do i get 0.9
6th Jun 2018, 5:09 PM
stephen haokip
stephen haokip - avatar
+ 1
at least one number should be of type double.ur code public class Program{ public static void main(String[] args){ System.out.println(9/10.0); }}
6th Jun 2018, 6:00 PM
Suhail Ahmad
Suhail Ahmad - avatar
+ 1
public class Program { public static void main(String[] args){ System.out.println(9/10); System.out.println(9.0/10); System.out.println(9/10.0); } } :::: OUTPUT :::: 0 0.9 0.9 ^That's implicit typecasting. When it sees that one of the values is a float or double, it assumes that you're implying both are of that type so it automatically converts it so you can do proper calculations with it.
6th Jun 2018, 6:11 PM
Fata1 Err0r
Fata1 Err0r - avatar
0
if you put only normal numbers like 1,2,3 it gonna treat them as integers, if you want to show 0.9 see cyk answer.
6th Jun 2018, 5:52 PM
Owenizedd
Owenizedd - avatar