I am working on some homework and my task is to write a program in C that uses a loop that counts from 1 to 10 and use the counter of that loop to calculate the multiples up to 5. I have the counting loop created that correctly counts to 10, but where I am stuck at the moment is on the second part of the task. I attempted to create a new loop, but it's not working the way that I would like it to.
#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;
}
}
This is what I am wanting it to look like:
You don't want to do this:
printf("%d", counter);
counter = counter * 1;
counter = counter * 2;
counter = counter * 3;
counter = counter * 4;
counter = counter * 5;
You're multiplying your counter by 1*2*3*4*5 = 120
every iteration! Instead, you want to keep your counter simply going 1, 2, 3, ...
and directly print the multiples:
int counter = 1;
while (counter <= 10) {
printf("%d\t %d\t %d\t %d\t %d\t %d\n",
counter * 1,
counter * 1,
counter * 2,
counter * 3,
counter * 4,
counter * 5);
counter++;
}