C program for multiplication table by addition

#include <stdio.h>

int main()
{
    int n, i, m;

    m = 0;

    for (n = 1; n <= 20; n++) {
        for (i = 1; i <= 10; i++) {
            m = n + m;
            printf("%d x %d = %d\n", n, i, m);
        }
    }

    return 0;
}

I’m trying to make a multiplication table from 1 - 20. I do it by (n * i) in the code. But I want to make it by addition.
The problem I am facing is,
After 1 x 10 = 10, in the 2 x 1 is not coming 2 its coming 12. I tried a lot to understand whats the problem (Around 3 hours) kindly help.

Note: I am a beginner in learning programming.

the correct code is,

#include <stdio.h>

int main()
{
    int n, i, m;

    m = 0;

    for (n = 1; n <= 20; n++) {
        m=0;
        for (i = 1; i <= 10; i++) {
            m = n + m;
            printf("%d x %d = %d\n", n, i, m);
        }
    }

    return 0;
}

you have to reinitialize the m by 0 before the inner loop.

1 Like

Thank You. :slightly_smiling_face:

2 Likes