0

Popsicle problem: why does my code not work for test 2 & 5?

int siblings = input.nextInt(); int popsicles = input.nextInt(); //your code goes here if (popsicles>siblings) { float x = popsicles / siblings; if (x == Math.ceil(x) && x == Math.floor(x)) { System.out.println("give away"); } else System.out.println("eat them yourself"); } else System.out.println("eat them yourself"); }

20th Nov 2021, 7:33 AM
Sean Lim
4 Antworten
+ 6
You need to use float variables for division to avoid losing decimals. Also, you can give popsicles to your siblings if you have same number of siblings and popsicles. float siblings = input.nextFloat(); float popsicles = input.nextFloat(); //your code goes here if (popsicles>=siblings) { ...
20th Nov 2021, 8:24 AM
Simba
Simba - avatar
+ 3
Sean Lim , we only need to check if popsicles can be evenly divided by siblings (without a remainder) by using a modulo division: .... if (popsicles % siblings == 0) { System.out.println("......."); } else { System.out.println("......."); } ....
20th Nov 2021, 9:36 AM
Lothar
Lothar - avatar
0
Please always tag the language you're asking about. https://code.sololearn.com/W3uiji9X28C1/?ref=app
20th Nov 2021, 1:07 PM
Simon Sauter
Simon Sauter - avatar
0
popsicles%siblings==0
20th Dec 2022, 5:09 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar