Home > Software design >  Generating multiplication table using for loop using Kotlin
Generating multiplication table using for loop using Kotlin

Time:10-08

I am trying to generate multiplication table using for loop in kotlin, here is my code:

       import java.util.Scanner

        fun main(args: Array<String>) {
              val read = Scanner(System.`in`)

              println("Enter any number:")
              var num = read.nextInt()

              for(i in 1..10)
                 var mul = num * i
                 println("$num * $i = $mul")
         }

But, after compiling this code, it showing me this error:

Multiplication.kt:11:26: error: unresolved reference: i println("$num * $i = $mul")

CodePudding user response:

for without brackets will be only valid for 1 following line. Since you have two lines, you must wrap it:

fun main(args: Array<String>) {
  val read = Scanner(System.`in`)

  println("Enter any number:")
  var num = read.nextInt()

  for(i in 1..10) { // <---
    var mul = num * i
    println("$num * $i = $mul")
  } // <---
}
  • Related