for (i = 2; i < input; i++){
for (d = 2; d < input; d++){
product = d*i;
printf("%d\n", product);
break;
}
}
This snippet of code is part of my solution to a homework question for my Intro to C class. The actual question is something involving prime numbers, but the solution that I thought of requires the use of for loops nested like this. I can't get them to behave correctly, though. I assume using an array would simplify things, but we haven't taken them up yet and are not allowed to use them in our solution. Anyway:
If, for example, my input is 10, my values of i should be {2,3,4,5,6,7,8,9}. My values for d should also be {2,3,4,5,6,7,8,9}.
What I expect this loop to do is multiply each i by the full loop of d:
2*2
2*3
2*4
.
.
.
2*9
3*2
3*3
.
.
.
.
and onwards. However, it is instead just multiplying i by d = 2, and I get the following:
4
6
8
10
.
.
.
20
What am I doing wrong?
break;the inner loop so it's only executed once...{}button above the edit box to indent the data by 4 spaces, which puts it into constant-width font and highlights it, as shown.