生成乘法表的 C++ 程式


乘法表用於對任何數字定義乘法運算。通常用於奠定十進位制數基本算術運算基礎。

任何數字的乘法表都寫到 10。在每一行中,顯示該數字與 1 到 10 的乘積。如下所示為 4 的乘法表示例 −

4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

生成給定數字乘法表的程式如下。

示例

 現場演示

#include <iostream>
using namespace std;
int main() {
   int n = 7, i;
   cout<<"The multiplication table for "<< n <<" is as follows:"<< endl;
   for (i = 1; i <= 10; i++)
   cout << n << " * " << i << " = " << n * i << endl;
   return 0;
}

輸出

The multiplication table for 7 is as follows:
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

在上述程式中,使用一個 for 迴圈從 1 到 10。在 for 迴圈的每次迭代中,該數字乘以 i 並顯示乘積。以下程式碼段對此進行了說明。

for (i = 1; i <= 10; i++)
cout << n << " * " << i << " = " << n * i <<endl;

更新於:24-6 月-2020

4K+ 瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告