如何使用Python、C++、JavaScript和C语言来显示一个数字的乘法表

196 阅读3分钟

在使用不同的语言进行编程时,你可以用几行循环的代码来打印一个数字的乘法表。但在不知道如何做的情况下这样做是很困难的。

不过不用担心,因为我们已经帮你解决了。在这篇文章中,你将学习如何使用Python、C++、JavaScript和C语言来打印一个数字的乘法表。

显示一个10以内的数字的乘法表

首先,让我们看看如何显示10以内的数字的乘法表。

问题陈述

你得到了一个数字num。你需要打印num到10的乘法表。例子。设num=5。5的乘法表。

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

显示10以内数字的乘法表的方法

你可以按照下面的方法来显示10以内数字的乘法表。

  1. 运行一个从1到10的循环。
  2. 在每个迭代中,将给定的数字乘以迭代号。例如,如果给定的数字是5,那么在第一次迭代中,5乘以1。 在第二次迭代中,5乘以2,以此类推。

显示10以内数字的乘法表的C++程序

下面是显示10以内的数字的乘法表的C++程序。

// 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;}

输出。

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

相关的。如何找到一个数组中所有元素的乘积

显示10以内数的乘法表的Python程序

下面是显示10以内的数字的乘法表的Python程序。

# 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)

输出。

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中使用For循环

显示10以内数的乘法表的JavaScript程序

下面是显示10以内数字的乘法表的JavaScript程序。

// 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);

输出。

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

显示10以内数字的乘法表的C语言程序

下面是显示10以内数字的乘法表的C语言程序。

// 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;}

输出。

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

显示指定范围内的数字的乘法表

当然,你不一定会坚持使用10及以下的乘法表。知道如何处理更高的乘法表也是有好处的,你会在下面找到你需要的所有信息。

问题陈述

你得到了一个数字 一个范围。你需要打印num在该范围内的乘法表。例子。设num=5,范围=14。

5的乘法表到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

显示一个数字在指定范围内的乘法表的方法

你可以按照下面的方法来显示一个数字在指定范围内的乘法表。

  1. 运行一个从1到范围的循环。
  2. 在每个迭代中,将给定的数字乘以迭代号。例如,如果给定的数字是5,那么在第一次迭代中,5乘以1。 在第二次迭代中,5乘以2,以此类推。

显示指定范围内数字的乘法表的C++程序

下面是一个C++程序,显示一个数字在指定范围内的乘法表。

// 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;}

输出。

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中使用While Loop

显示一个给定范围内的数字的乘法表的Python程序

下面是一个Python程序,用于显示一个给定范围内的数字的乘法表。

# 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)

输出。

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中使用列表的循环

显示指定范围内的数字的乘法表的JavaScript程序

下面是显示一个数字在给定范围内的乘法表的JavaScript程序。

// 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);

输出。

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语言程序

下面是显示指定范围内的数字的乘法表的C语言程序。

// 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;}

输出。

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

理解基本的编程原理,成为一个更好的程序员

在这篇文章中,你学到了如何利用循环的力量在几行代码中显示一个数字的乘法表。在几乎所有的编程语言中,你都可以用几行代码显示乘法表。

如果你想成为一个更好的程序员,你必须遵循基本的编程原则,如KISS(保持简单,愚蠢),DRY(不要重复自己),单一责任,YAGNI(你不会需要它),开放/封闭,组成大于继承,等等。我们已经有了关于这些的指南,为什么不接下来去那里呢?