Algorithm
-
Get the Number from the User:
- Ask the user to input a number for which they want to generate the multiplication table.
- Store the entered number in a variable.
-
Validate Input:
- Check if the entered value is a valid number.
- If not, prompt the user to enter a valid number.
-
Generate and Display Multiplication Table:
- Use a loop (for loop or while loop) to iterate from 1 to 10 (or any desired range).
- Inside the loop, calculate the product of the entered number and the current loop variable.
- Display the multiplication table entry in the format:
${number} x ${current loop variable} = ${product}
.
Code Examples
#1 Code Example- Multiplication Table Up to 10
Code -
Javascript Programming
// program to generate a multiplication table
// take input from the user
const number = parseInt(prompt('Enter an integer: '));
//creating a multiplication table
for(let i = 1; i < = 10; i++) {
// multiply i with number
const result = i * number;
// display the result
console.log(`${number} * ${i} = ${result}`);
}
Copy The Code &
Try With Live Editor
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
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
#2 Code Example- Multiplication Table Up to a Range
Code -
Javascript Programming
/* program to generate a multiplication table
upto a range */
// take number input from the user
const number = parseInt(prompt('Enter an integer: '));
// take range input from the user
const range = parseInt(prompt('Enter a range: '));
//creating a multiplication table
for(let i = 1; i < = range; i++) {
const result = i * number;
console.log(`${number} * ${i} = ${result}`);
}
Copy The Code &
Try With Live Editor
Output
Enter an integer: 7
Enter a range: 5
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
Enter a range: 5
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
Demonstration
JavaScript Programing Example to Display the Multiplication Table-DevsEnv