九九乘法表 从键盘输入n(n为1~9),输出九九乘法表的前n行,每个式子按“a * b = c”形式输出,其中a和b各占1位列宽,c占4位列宽并左对齐。用C++代码实现。
时间: 2024-03-01 07:54:23 浏览: 54
好的,下面是实现该功能的C++代码:
```cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cout << "请输入n的值(1~9):";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " * " << i << " = " << left << setw(4) << i*j << " ";
}
cout << endl;
}
return 0;
}
```
运行效果示例:
```
请输入n的值(1~9):5
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 =12 4 * 4 =16
1 * 5 = 5 2 * 5 =10 3 * 5 =15 4 * 5 =20 5 * 5 =25
```
相关问题
10、从键盘输入1-9之问一个整数n,输出九九乘法表的前n行。
以下是实现该功能的 Python 代码:
```python
n = int(input("请输入一个整数 n(1 ≤ n ≤ 9):"))
for i in range(1, n+1):
for j in range(1, i+1):
print("{}*{}={}\t".format(j, i, i*j), end="")
print()
```
运行程序后,会提示用户输入一个整数 n,然后输出九九乘法表的前 n 行。例如,如果用户输入 5,则输出以下内容:
```
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
```
从键盘输入n(n<10),输出九九乘法表的前n行的结果
以下是 Python 代码实现:
```python
n = int(input("请输入 n(n<10):"))
# 打印九九乘法表前 n 行
for i in range(1, n+1):
for j in range(1, i+1):
print(j, "*", i, "=", j*i, end="\t")
print()
```
输入示例:
```
请输入 n(n<10):5
```
输出示例:
```
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)