用for循环输出九九乘法表
时间: 2024-05-24 15:06:16 浏览: 21
以下是两种用for循环输出九九乘法表的方法:
方法一:利用双循环来打印九九乘法表
```c
#include<stdio.h>
int main() {
int i,j;
for(i=1;i<=9;i++)
for(j=1;<=i;j++) {
if(j==i) printf("%d*%d=%2d\n",j,i,i*j);//打印后换行
else printf("%d*%d=%2d\t",j,i,i*j); }
return 0;
}
```
方法二:利用三重循环来打印九九乘法表
```c
#include<stdio.h>
int main() {
int i,j,k;
for(i=1;i<10;i++) {
for(j=1;j<i;j++) printf("\t");//在每行前面空格
for(k=i;k<10;k++) {
if(k==9) printf("%d*%d=%2d\n",i,k,i*k);//乘到九后换行
else printf("%d*%d=%2d\t",i,k,i*k);
}
}
return 0;
}
```
相关问题
python用for循环输出九九乘法表
可以使用嵌套的for循环来输出九九乘法表,具体代码如下:
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
输出结果如下:
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
jsfor循环输出九九乘法表
九九乘法表可以用不同的循环结构来实现,下面是三种常见的方式:
方法一、使用while循环:
```javascript
var i = 1, j = 1;
while (i <= 9) {
while (j <= i) {
document.write(j + " * " + i + " = " + i * j + " ");
j++;
}
j = 1;
i++;
document.write('<br>');
}
```
方法二、使用do-while循环:
```javascript
var i = 1, j = 1;
do {
do {
document.write(j + " * " + i + " = " + i * j + " ");
j++;
} while (j <= i);
j = 1;
i++;
document.write('<br>');
} while (i <= 9);
```
方法三、使用for循环:
```javascript
for (var i = 1; i <= 9; i++) {
for (var j = 1; j <= i; j++) {
document.write(j + " * " + i + " = " + i * j + " ");
}
document.write('<br>');
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)