0

Can someone help me with this question. I don't know how to count all inputs. KOTLIN

The given code uses an infinite while loop to take continuous user input. During each iteration a number is taken from the input. You need to fix the program to stop the loop when the user enters 0 and output the number of inputs taken before that. Sample Input: 42 1 66 0 Sample Output: 3

8th Jun 2021, 11:17 PM
Yuzay Yuzay
Yuzay Yuzay - avatar
4 Réponses
+ 3
Not exact code but an idea, count=0 while(true) or while(1) : a=input; if(a==0) : //print the count variable break else: //increment count by 1
8th Jun 2021, 11:25 PM
Abhay
Abhay - avatar
+ 3
Use this code but not blindly, try to understand the codes, try on yourself : fun main(args: Array<String>) { var count = 0 while(true) { var input = readLine()!!.toInt() if(input==0){ println(count) break }else{ count+=1 } } }
14th Jul 2021, 9:32 AM
Sowmiyashree S
Sowmiyashree S - avatar
+ 1
//you can try this. fun main(args: Array<String>){ var num=mutableListOf<Int>() while(true){ var input=readLine()!!.toInt() if(input==0){ break }else{ num.add(input) } } println(num.size) }
23rd Jul 2021, 1:13 PM
Coding King.
Coding King. - avatar