INTERNET

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

0

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 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 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 10void printTable(int num){ for (int i = 1; i <= 10; ++i) { cout << num << " * " << i << " = " << num * i << endl; }}// Driver Codeint main(){ int num = 5; cout << "Number: " << num << endl; cout << "Multiplication table of " << num << endl; printTable(num);return 0;}

Output:

Number: 5Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 50

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 10def printTable(num):    for i in range(1, 11):        print(num, "*", i, " =", num*i)# Driver Codenum = 5print("Number:", num)print("Multiplication table of", num)printTable(num)

Output:

Number: 5Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 50

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 10function printTable(num) { for (let i = 1; i <= 10; ++i) { document.write(num + " * " + i + " = " + num * i + "<br>"); }}// Driver Codevar num = 5;document.write("Number: " + num + "<br>");document.write("Multiplication table of " + num + "<br>");printTable(num);

Output:

Number: 5Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 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 10void printTable(int num){ for (int i = 1; i <= 10; ++i) { printf("%d * %d = %d \⁠n", num, i, num*i); }}// Driver Codeint main(){ int num = 5; printf("Number: %d \⁠n", num); printf("Multiplication table of %d \⁠n", num); printTable(num); return 0;}

Output:

Number: 5Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 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 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 505 * 11 = 555 * 12 = 605 * 13 = 655 * 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 numbervoid printTable(int num, int range){ for (int i = 1; i <= range; ++i) { cout << num << " * " << i << " = " << num * i << endl; }}// Driver Codeint 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: 5Range: 14Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 505 * 11 = 555 * 12 = 605 * 13 = 655 * 14 = 70

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 numberdef printTable(num, r):    for i in range(1, r+1):        print(num, "*", i, " =", num*i)# Driver Codenum = 5r = 14print("Number:", num)print("Range:", range)print("Multiplication table of", num)printTable(num, r)

Output:

Number: 5Range: 14Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 505 * 11 = 555 * 12 = 605 * 13 = 655 * 14 = 70

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 numberfunction printTable(num, range) { for (let i = 1; i <= range; ++i) { document.write(num + " * " + i + " = " + num * i + "<br>"); }}// Driver Codevar 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: 5Range: 14Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 505 * 11 = 555 * 12 = 605 * 13 = 655 * 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 numbervoid printTable(int num, int range){ for (int i = 1; i <= range; ++i) { printf("%d * %d = %d \⁠n", num, i, num*i); }}// Driver Codeint 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: 5Range: 14Multiplication table of 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 505 * 11 = 555 * 12 = 605 * 13 = 655 * 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?

admin

Airbnb Promises to House 20,000 Afghan Refugees for Free

Previous article

The 5 Best Ways to Deal With Screen Time Exhaustion

Next article

You may also like

Comments

Leave a reply

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

More in INTERNET

Login/Sign up