Halloween candy (code coach riddle) - who can tell me whats wrong with this code? - JAVA
I'm trying to solve Code Coach task (easy). My way of thinking here is: 1. Get no. of houses 2. Extract percentage 3. Round the outcome 4. Produce result Got it on code playground, where I added additional outputs for monitoring. It works nice with any no. of houses, but won't work as solution (I modify it beforehand to get only required percentage outcome after rounding). Why? CODE: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int houses = input.nextInt(); //your code goes here for(int x=3; x<=houses; x++) { double no1 = (2/(double)x); double no2 = no1*1000; int num = (int)no2; System.out.println(x); System.out.println(no1); System.out.println(no2); System.out.println(num); if (num % 10 >= 5) {System.out.println((num/10) + 1);} else {System.out.println(num/10);} System.out.println(""); } } }