Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiplication of numbers from a to b

I need to write algorithm, that multiplies numbers from a to b without input (scanf). Like this:

a = 2;
b = 6;

2 * 3
2 * 4
...
2 * 6

I have my algorithm:

void main()
{
    int dist = 1;
    int a = 2;
    int b = 5;
    for (int i = a; a <= b; a++) {
        printf("%d", a * a++);
    }
}

but it doesn’t work correct

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because you are increasing a (a++) two times in your example above. Also you mixed up a and i a little bit. Correct one is:

int a = 2;
int b = 5;
for (int i = a; i <= b; i++)
{
    printf("%d * %d = %d\n", a, i, a * i);
}

which prints:

2 * 2 = 4

2 * 3 = 6

2 * 4 = 8

2 * 5 = 10

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading