+ 1

Parking Fee Problem

I am wondering what's wrong with my code. No syntax errors as well. But it is showing 'No Output' for this code. It will be good if someone could help me out fix this. CODE:- ```fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours<=5) { total=hours println(total) } else if(hours>5&&hours<24) { total=5+((hours-5)*0.5) println(total) } else if(hours>=24) { total=15*(hours/24)+((hours-24)*0.5) println(total) } } ```

29th Mar 2021, 10:11 AM
Vishnuvasan Srinivasan
Vishnuvasan Srinivasan - avatar
7 odpowiedzi
+ 3
Vishnuvasan Srinivasan Here is my solution : fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours > 24) { total += 15 * (hours / 24) + 0.5 * (hours % 24) } else if(hours < 24 && hours > 5) { total += (hours - 5) * 0.5 + 5 } else { total += hours * 1 } println(total) }
30th Mar 2021, 1:06 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 3
Vishnuvasan Srinivasan You are getting no output because there is an error. hours is an integer and total is double so you can't directly assign integer to double. To resolve this issue just do like this total= hours * 1.0
29th Mar 2021, 10:30 AM
A͢J
A͢J - avatar
+ 2
Vishnuvasan Srinivasan There should be % not - in case of 24 because suppose there is 49 hours and you have to pay 15 / hours so there will be 24 * 15 + (49 % 24) * 0.5 but if you use - then it would be 24 * 15 + (49 - 24) * 0.5 which is wrong. So here is right solution total = 15 * (hours / 24) + (hours % 24) * 0.5
29th Mar 2021, 10:21 AM
A͢J
A͢J - avatar
+ 2
I try to make as simple as i can. This is mine fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours <= 5){ total = hours * 1.0 } else if (hours > 5 && hours <24){ total = 5.0+((hours-5.0)*0.5) } else{ total = ((hours/24)*15.0) + ((hours%24.0)*0.5) } println(total) }
18th Jul 2021, 12:24 PM
Mega Elisa
0
You can follow this approach :- fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 var take:Double total = when{ hours<=0 -> 0.0 hours<=5 -> hours*1.0 hours>5 && hours<24 -> hours*1.0 - (hours-5)*0.5 hours>=24 -> (hours/24*15) + (hours%24*0.5) else -> 0.0 } println(total) }
5th Apr 2021, 3:58 PM
Shashi Nirala
Shashi Nirala - avatar
0
fun main(args: Array<String>) { var x = readLine()!!.toInt() var y : Double = 0.5 when { x <= 5 -> println( x*1 ) x > 5 && x < 24 -> println(2.5+(x*y)) x > 24 -> println( (x%24)*y + (x/24)*15) } }
7th Nov 2021, 11:23 PM
woney
0
Try this code out:- fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours<=5){ total += hours println(total) } else if(hours>5&&hours<24){ total += 5 + (hours - 5) * 0.5 println(total) } else if(hours==24){ total += 15 println(total) } else if(hours>24){ total += (hours/24)*15+(hours%24)*0.5 println(total) } }
19th Jan 2022, 9:16 PM
Alharith Albayati