Kotlin program to print the multiplication table of given number

Kotlin | printing multiplication table: Here, we are going to learn how to print the multiplication table of a given number in Kotlin programming language? Submitted by IncludeHelp, on April 17, 2020

Given an integer number, we have to print its multiplication table.

Kotlin - Print the multiplication table of given number

To print a multiplication table, we run a loop starting from 1 to 10 and print the multiplication of the number with the loop variable/counter.

Program to print the multiplication table in Kotlin

In the below program, we are creating a Kotlin program for printing the multiplication table.

package com.includehelp.basic

import java.util.*

// Main function entry point of program
fun main(args: Array<String>) {

    val sc = Scanner(System.`in`)
    println("Enter Number: ")
    val num: Int = sc.nextInt() 

    // for loop to print multiplication table of given number
    for (i in 1..10) {
        val result = num * i
        println("$num*$i = $result")
    }
}

Output

Enter Number:
5
5*1 = 5
5*2 = 10
5*3 = 15
5*4 = 20
5*5 = 25
5*6 = 30
5*7 = 35
5*8 = 40
5*9 = 45
5*10 = 50
------
Run 2:
Enter Number:
12
12*1 = 12
12*2 = 24
12*3 = 36
12*4 = 48
12*5 = 60
12*6 = 72
12*7 = 84
12*8 = 96
12*9 = 108
12*10 = 120

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.