Kotlin Program to Generate Multiplication Table

In this program, we will learn to code the Kotlin Program to Generate Multiplication Table. Let’s understand “What is a multiplication table” and then we will see How to Generate a Multiplication table of any number in KotlinProgramming Language.

We will solve this problem using Two Approaches and we’ll display the Multiplication Table of a Number using both For Loop and While Loop.

Now, Let’s see the Kotlin Program to Generate Multiplication Table using For Loop.

Kotlin Program to Generate Multiplication Table

// Kotlin Program to Generate Multiplication Table

import java.util.Scanner;

fun main(args: Array<String>) {
    
    var sc = Scanner(System.`in`);
    
    println("Enter a Number : ");
    var num = sc.nextInt();
    
    println("Multiplication Table =>");
    for(i in 1..10){
        var prod = num * i;
        println("$num * $i = $prod");
    }
    
}

Output

Enter a Number : 7

Multiplication Table =>
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

How Does This Program Work ?

    var sc = Scanner(System.`in`);
    
    println("Enter a Number : ");
    var num = sc.nextInt();

In this program, we take the input of the integer number from the user, using the Scanner class in Kotlin. We import the Scanner Class using import java.util.Scanner;

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

Then, we create the For Loop to iterate in the range of 1 to 10. In Kotlin the syntax to define a For Loop is Different from that of Java Programming Language and other programming languages.

To iterate over a range of numbers we have to use the range expression in the For Loop in Kotlin.

Then, inside the body of the Loop, we calculate the product of the number i.e. num * i and print the Multiplication table using the println() method in Kotlin.

Here, to print the value of the variable along with the string we use the $ operator. The concept is same as that in JavaScript.
e.g. println("$num * $i = $prod");

This is the Kotlin Program to Generate Multiplication Table using For Loop. Now, let’s see the second method to do so.

Kotlin Program to Generate Multiplication Table Using While Loop

// Kotlin Program to Generate Multiplication Table

import java.util.Scanner;

fun main(args: Array<String>) {
    
    var sc = Scanner(System.`in`);
    
    println("Enter a Number : ");
    var num = sc.nextInt();
    
    var i = 1;
    println("Multiplication Table =>");
    while(i <= 10){
        var prod = num * i;
        println("$num * $i = $prod");
        i++;
    }
    
}

Output

Enter a Number : 18

Multiplication Table =>
18 * 1 = 18
18 * 2 = 36
18 * 3 = 54
18 * 4 = 72
18 * 5 = 90
18 * 6 = 108
18 * 7 = 126
18 * 8 = 144
18 * 9 = 162
18 * 10 = 180

This is the Kotlin Program to Generate Multiplication Table using While Loop.

Conclusion

I hope after going through this post, you understand Kotlin Program to Generate Multiplication Table. We discussed two approaches in this to generate the Multiplication Table in Kotlin. We have used Both For Loop and While Loop.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Learn More:

Leave a Comment

Your email address will not be published. Required fields are marked *