Reputation: 55
I want to build a counting table from 1 to 20 using inner for loop but instead of using multiplication, I want to use summation to build up my answer.
I wrote this code and for only number 1, I can see the correct answer.
From number 2, I see that it adds up from the 10th multiplication.
I want to correct my logic error here in this code and any help is highly appreciated.
#include <stdio.h>
int main()
{
int m , n=1;
int i;
m = 0;
for(n = 1; n <= 20; n++){
for(i = 1; i <= 10; i++){
m = m + n;
printf("%d * %d = %d\n", n, i, m);
}
}
return 0;
}
Upvotes: 0
Views: 30
Reputation: 1986
#include <stdio.h>
int main(void)
{
int m, n, i;
for (n = 1; n <= 20; n++) {
// reset m before additions
m = 0;
for (i = 1; i <= 10; i++) {
m += n;
printf("%d * %d = %d\n", n, i, m);
}
}
return (0);
}
Upvotes: 1