+ 1

[Solved] Why is this code not working properly?

I am pretty new to Java and I made a code in which a number is inputted, divided by eleven and multiplied by 100. The answer must be outputted to the nearest integer... Sample input:5 Expected output:45 Real output:0 https://sololearn.com/compiler-playground/cicNiI31xopC/?ref=app

19th Feb 2024, 4:15 PM
Shreyash Roopnah
Shreyash Roopnah - avatar
2 Réponses
+ 2
Try double y=(x/11.0)*100; or take the input number x as double In order to get a floating point number from a division, one of the numbers needs to be a double.
19th Feb 2024, 4:40 PM
Lisa
Lisa - avatar
0
import java.util.Scanner; public class main { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int x= sc.nextInt(); float y=(x/11.0f)*100; System.out.println(y); } } The ans show the wrong because of is consider 11 as integer then division is (5/11) become the 0 then 0*100=0 you try above program
19th Feb 2024, 6:19 PM
YevaCoder
YevaCoder - avatar