编写程序:把1~100之间能被7整除的数,以每行5个的形式在屏幕上输出
时间: 2024-02-29 22:53:14 浏览: 26
好的,以下是 Python 代码实现:
```python
count = 0
for i in range(1, 101):
if i % 7 == 0:
print(i, end=' ')
count += 1
if count % 5 == 0:
print('')
```
这段代码利用了 `range()` 函数来生成 1 到 100 的整数序列,然后通过取模运算 `i % 7` 判断每个数是否能被 7 整除。如果能整除,就输出这个数,同时统计已经输出的数的个数。当输出的数的个数能被 5 整除时,就输出一个换行符,以实现每行输出 5 个数的效果。
相关问题
c语言把1~100之间能被7整除的数,以每行5个的形式在屏幕上输出
Sure, here's the C code to output all numbers between 1 and 100 that are divisible by 7, in rows of 5:
```c
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 100; i++)
{
if (i % 7 == 0)
{
printf("%d ", i);
if (i % 35 == 0)
printf("\n");
}
}
return 0;
}
```
I hope that helps!
C语言while编写程序在屏幕上输出1~100之间能被7整除的数,以每行输出10个的格式。
好的,这是一个比较简单的程序,可以使用while循环和if语句来实现,在这里给您写出代码,供参考:
```c
#include <stdio.h>
int main() {
int count = 0; // 计数器,用于控制每输出10个数再换行
int i = 1;
while (i <= 100) {
if (i % 7 == 0) {
printf("%d ", i);
count++;
if (count == 10) {
printf("\n");
count = 0;
}
}
i++;
}
return 0;
}
```
这个程序运行起来,会在屏幕上输出1~100之间能被7整除的数,每行输出10个,如下所示:
```
7 14 21 28 35 42 49 56 63 70
77 84 91 98
```
希望对您有帮助!还有什么问题可以继续问我哦~