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); } }
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.
0
Ebney Samrat
import java.util.Scanner;
No brackets after Scanner
0
Ah ok.