Why is the output 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the output 0?

double p = 2/10; Why is the output 0.0 instead of 0.2? I've been trying to figure out why on my own but I couldn't really understand why it gives out 0 any explanation?

12th Mar 2021, 5:50 PM
Bãrt Simpson
Bãrt Simpson - avatar
2 Answers
+ 6
khalid raji Because here 2 and 10 will be treat as integer. So output will be 0. If you want to get in double then just do typecast like double p = (double) 2 / 10;
12th Mar 2021, 5:52 PM
A͢J
A͢J - avatar
+ 2
I Am AJ ! It worked ! Thank you 😊
12th Mar 2021, 5:55 PM
Bãrt Simpson
Bãrt Simpson - avatar