【发布时间】:2021-05-20 18:01:56
【问题描述】:
我正在做一些作业,我的任务是用 C 编写一个程序,该程序使用一个从 1 计数到 10 的循环,并使用该循环的计数器来计算最多为 5 的倍数。我创建了计数循环正确计数到 10,但我现在卡在任务的第二部分。我试图创建一个新循环,但它没有按照我想要的方式工作。
#include <stdio.h>
int main (void) {
int counter = 1;
// heading
puts("Number\t 1st\t 2nd\t 3rd\t 4th\t 5th");
// loop that counts to 10
while (counter <= 10) {
printf("%d\n", counter);
counter++; // adds +1 to the counter
}
// stuck on this part
// loop that attempts to take the 10 numbers from prior loop and display their multiples up to 5 times
while (counter <=10) {
printf("%d", counter);
counter = counter * 1;
counter = counter * 2;
counter = counter * 3;
counter = counter * 4;
counter = counter * 5;
}
}
这就是我想要的样子:
【问题讨论】:
标签: c loops counter multiplication