+ 2

Why my code is passing only 3 test cases but fails in others out of 6? Could anyone help please?

QUESTION: You are making a car parking software that needs to calculate and output the amount due based on the number of hours the car was parked. The fee is calculated based on the following price structure: - the first 5 hours are billed at $1 per hour. - after that, each hour is billed at $0.5 per hour. - for each 24 hours, there is a flat fee of $15. This means, that, for example, if a car parked for 26 hours, the bill should be 15+(2*0.5) = 16.0, because it was parked for 24 hours plus 2 additional hours. Sample Input: 8 Sample Output: 6.5 (Output must be in Double data type) CODE: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours <= 5) total = 1.0*hours else if(hours > 5 && hours < 24) total = 5.0+(0.5*(hours-5.0)) else if(hours == 24) total = 15.0 else if(hours > 24) total = 15.0+(0.5*(hours - 24.0)) println(total) }

5th Jul 2023, 8:00 PM
Mian Muhammad Waleed Asif
Mian Muhammad Waleed Asif - avatar
2 Réponses
+ 6
Mian Muhammad Waleed Asif when hours > 24 total = 15.0 * hours / 24 + 0.5 * hours % 24 if hours is 50 then 15/day = 2 * 15 = 30 now remaining hours 50 % 24 = 2 and 0.5/hours = 0.5 * 2 = 1.0
5th Jul 2023, 8:32 PM
A͢J
A͢J - avatar
+ 4
You can test your code in SL Playground for each diferent input as in the task description and see if the output is as expected.
5th Jul 2023, 8:22 PM
JaScript
JaScript - avatar