JS Example for Beginners: JavaScript Program to Display the Multiplication Table

Hits: 16

(JavaScript programming Example for Beginners)

JavaScript Program to Display the Multiplication Table

In this example, you will learn to generate the multiplication table of a number in JavaScript.


Example 1: Multiplication Table Up to 10

// program to generate a multiplication table

// take input from the user
let number = parseInt(prompt('Enter an integer: '));

//creating a multiplication table
for(let i = 1; i <= 10; i++) {

    // multiply i with number
    result = i * number;

    // display the result
    console.log(`${number} * ${i} = ${result}`);
}

Output

Enter an integer: 3
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30

In the above program, the user is prompted to enter an integer value. Then, the for loop is used to iterate through 1 to 10 to create a multiplication table.


Example 2: Multiplication Table Up to a Range

/* program to generate a multiplication table
upto a range */

// take number input from the user
let number = parseInt(prompt('Enter an integer: '));

// take range input from the user
let range = parseInt(prompt('Enter a range: '));

//creating a multiplication table
for(let i = 1; i <= range; i++) {
    result = i * number;
    console.log(`${number} * ${i} = ${result}`);
}

Output

Enter an integer: 7
Enter a range: 5
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

In this above example, the user is prompted to enter an integer and also a range to which they want to create a multiplication table.

The user enters an integer(here 7) and a range(here 5). Then a multiplication table is created using a for loop to that range.

 

JS Example for Beginners: JavaScript Program to Display the Multiplication Table

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Leave a Reply

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