Looking For Anything Specific?

How to Display the Multiplication Table of a Number Using Python, C++, JavaScript, and C

When programming using different languages, you can print the multiplication table of a number with few lines of code using loops. But doing this without knowing how to is difficult.

Don't worry, though, because we've got you covered. In this article, you'll learn how to print the multiplication table of a number using Python, C++, JavaScript, and C.

Display Multiplication Table of a Number Up to 10

First, let's look at how to display multiplication tables for numbers up to 10.

Problem Statement

You're given a number num. You need to print the multiplication table of num up to 10. Example: Let num = 5. Multiplication table of 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

Approach to Display the Multiplication Table of a Number Up to 10

You can follow the approach below to display the multiplication table of a number up to 10:

  1. Run a loop from 1 to 10.
  2. In each iteration, multiply the given number by iteration no. For example- If the given number is 5, therefore on the 1st iteration, multiply 5 by 1. On the 2nd iteration, multiply 5 by 2, and so on.

C++ Program to Display the Multiplication Table of a Number Up to 10

Below is the C++ program to display the multiplication table of a number up to 10:

// C++ program to print the multiplication table of a number up to 10
#include <iostream>
using namespace std;
// Function to print the multiplication table of a number up to 10
void printTable(int num)
{
for (int i = 1; i <= 10; ++i)
{
cout << num << " * " << i << " = " << num * i << endl;
}
}
// Driver Code
int main()
{
int num = 5;
cout << "Number: " << num << endl;
cout << "Multiplication table of " << num << endl;
printTable(num);
return 0;
}

Output:

Number: 5
Multiplication table of 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

Related: How to Find the Product of All Elements in an Array

Python Program to Display the Multiplication Table of a Number Up to 10

Below is the Python program to display the multiplication table of a number up to 10:

# Python program to print the multiplication table of a number up to 10
# Function to print the multiplication table of a number up to 10
def printTable(num):
for i in range(1, 11):
print(num, "*", i, " =", num*i)

# Driver Code
num = 5
print("Number:", num)
print("Multiplication table of", num)
printTable(num)

Output:

Number: 5
Multiplication table of 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

Related: How to Use For Loops in Python

JavaScript Program to Display the Multiplication Table of a Number Up to 10

Below is the JavaScript program to display the multiplication table of a number up to 10:

// JavaScript program to print the multiplication table of a number up to 10
// Function to print the multiplication table of a number up to 10
function printTable(num) {
for (let i = 1; i <= 10; ++i) {
document.write(num + " * " + i + " = " + num * i + "<br>");
}
}
// Driver Code
var num = 5;
document.write("Number: " + num + "<br>");
document.write("Multiplication table of " + num + "<br>");
printTable(num);

Output:

Number: 5
Multiplication table of 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

C Program to Display the Multiplication Table of a Number Up to 10

Below is the C program to display the multiplication table of a number up to 10:

// C program to print the multiplication table of a number up to 10
#include <stdio.h>
// Function to print the multiplication table of a number up to 10
void printTable(int num)
{
for (int i = 1; i <= 10; ++i)
{
printf("%d * %d = %d \⁠n", num, i, num*i);
}
}
// Driver Code
int main()
{
int num = 5;
printf("Number: %d \⁠n", num);
printf("Multiplication table of %d \⁠n", num);
printTable(num);
return 0;
}

Output:

Number: 5
Multiplication table of 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

Display Multiplication Table of a Number Up to a Given Range

Of course, you won't necessarily stick to multiplication tables that are 10 and below. It pays to know how to do so for higher ones, too, and you'll find all the information you need below.

Problem Statement

You're given a number num and a range. You need to print the multiplication table of num up to that range. Example: Let num = 5 and range=14.

Multiplication table of 5 up to range 14:

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
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70

Approach to Display the Multiplication Table of a Number Up to a Given Range

You can follow the approach below to display the multiplication table of a number up to a given range:

  1. Run a loop from 1 to range.
  2. In each iteration, multiply the given number by iteration no. For example- If the given number is 5, therefore on the 1st iteration, multiply 5 by 1. On the 2nd iteration, multiply 5 by 2, and so on.

C++ Program to Display the Multiplication Table of a Number Up to a Given Range

Below is the C++ program to display the multiplication table of a number up to a given range:

// C++ program to print the multiplication table of a number
#include <iostream>
using namespace std;
// Function to print the multiplication table of a number
void printTable(int num, int range)
{
for (int i = 1; i <= range; ++i)
{
cout << num << " * " << i << " = " << num * i << endl;
}
}
// Driver Code
int main()
{
int num = 5;
int range = 14;
cout << "Number: " << num << endl;
cout << "Range: " << range << endl;
cout << "Multiplication table of " << num << endl;
printTable(num, range);
return 0;
}

Output:

Number: 5
Range: 14
Multiplication table of 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
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70

Related: How To Use a While Loop in Python

Python Program to Display the Multiplication Table of a Number Up to a Given Range

Below is the Python program to display the multiplication table of a number up to a given range:

# Python program to print the multiplication table of a number
# Function to print the multiplication table of a number
def printTable(num, r):
for i in range(1, r+1):
print(num, "*", i, " =", num*i)

# Driver Code
num = 5
r = 14
print("Number:", num)
print("Range:", range)
print("Multiplication table of", num)
printTable(num, r)

Output:

Number: 5
Range: 14
Multiplication table of 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
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70

Related: How to Use Loops With Lists in Python

JavaScript Program to Display the Multiplication Table of a Number Up to a Given Range

Below is the JavaScript program to display the multiplication table of a number up to a given range:

// JavaScript program to print the multiplication table of a number
// Function to print the multiplication table of a number
function printTable(num, range) {
for (let i = 1; i <= range; ++i) {
document.write(num + " * " + i + " = " + num * i + "<br>");
}
}
// Driver Code
var num = 5;
var range = 14;
document.write("Number: " + num + "<br>");
document.write("Range: " + range + "<br>");
document.write("Multiplication table of " + num + "<br>");
printTable(num, range);

Output:

Number: 5
Range: 14
Multiplication table of 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
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70

C Program to Display the Multiplication Table of a Number Up to a Given Range

Below is the C program to display the multiplication table of a number up to a given range:

// C program to print the multiplication table of a number
#include <stdio.h>
// Function to print the multiplication table of a number
void printTable(int num, int range)
{
for (int i = 1; i <= range; ++i)
{
printf("%d * %d = %d \⁠n", num, i, num*i);
}
}
// Driver Code
int main()
{
int num = 5;
int range = 14;
printf("Number: %d \⁠n", num);
printf("Range: %d \⁠n", range);
printf("Multiplication table of %d \⁠n", num);
printTable(num, range);
return 0;
}

Output:

Number: 5
Range: 14
Multiplication table of 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
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70

Understand Basic Programming Principles to Become a Better Programmer

In this article, you learned how to display the multiplication table of a number in few lines of code using the power of loops. In almost every programming language, you can display the multiplication table in few lines of code.

If you want to become a better programmer, you must follow the basic programming principles like KISS (Keep It Simple, Stupid), DRY (Don't Repeat Yourself), Single Responsibility, YAGNI (You Aren't Going to Need It), Open/Closed, Composition Over Inheritance, and so on. We've got guides on these, so why not head over there next?


Post a Comment

0 Comments