Kotlin Program to Generate Multiplication Table


In this article, we will understand how to print a multiplication table. Multiplication table is created by iterating the required input 10 times using a for loop and multiplying the input value with numbers from 1 to 10 in each iteration.

Below is a demonstration of the same −

Suppose our input is −

Input : 16

The desired output would be −

The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Algorithm

  • Step 1 − START

  • Step 2 − Declare two integer values namely myInput and i.

  • Step 3 − Define the value

  • Step 4 − Iterate from 1 to 10 using a for-loop, and in each iteration, multiply the numbers 1 to 10 with the input.

  • Step 5 − Display the resulting value in after each iteration.

  • Step 6 − Stop

Example 1

In this example, we will generate Multiplication Table using for loop. First, declare and initialize the input for which you want the multiplication table −

val myInput = 16

Now, use a for loop from 1 to 10 since we want to display a table −

for (i in 1..10) { val product = myInput * i println("$myInput * $i = $product") }

Let us now see the complete example to generate Multiplication Table using for loop −

fun main() { val myInput = 16 println("The input is defined as $myInput") println("
The multiplication table of $myInput"
) for (i in 1..10) { val product = myInput * i println("$myInput * $i = $product") } }

Output

The input is defined as 16
The multiplication table of 16
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Example 2

In this example, we will generate Multiplication Table −

fun main() { val myInput = 16 println("The input is defined as $myInput") multiplicationTable(myInput) } fun multiplicationTable(myInput: Int) { println("
The multiplication table of $myInput"
) for (i in 1..10) { val product = myInput * i println("$myInput * $i = $product") } }

Output

The input is defined as 16
The multiplication table of 16
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Updated on: 13-Oct-2022

671 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements