0

Why I get output:0?

import java.util.Scanner; public class Program{ public static void main(String [] args){ Scanner input=new Scanner(System.in); int x=input.nextInt(); int y=2; int p=(y/x)*100; System.out.println(p); } }

14th Aug 2020, 5:56 AM
Ebney Samrat
3 Respuestas
+ 3
In Java, integer/integer always results in integer. fractional part is discarded. example : ``` System.out.println(5/2); //2 ``` Here 5 and 2 are both integer literals. So result will also be integer (neither float nor double). 5/2 should be 2.5 but 0.5 (fractional part) is discarded as it's an integer value. Hope you can understand why your example shows 0. If you can't figure out , feel free to ask.
14th Aug 2020, 6:06 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Ebney Samrat import java.util.Scanner; No brackets after Scanner
14th Aug 2020, 6:08 AM
Baratov Hojiakbar
Baratov Hojiakbar - avatar
0
Ah ok.
14th Aug 2020, 6:09 AM
Ebney Samrat