How do I force "while"?

1

This program allows you to calculate the table of up to 10 up to a given number x. But in the cycle of while after the first iteration it does not enter and jumps to the end. For example, if I enter 2 my output is:

Introduza o numero da tabuada que quer!

3
tabuada do 0

 0x3=0

 1x3=0

 2x3=0

 3x3=0

 4x3=0

 5x3=0

 6x3=0

 7x3=0

 8x3=0

 9x3=0

 10x3=0

 tabuada do 1


 tabuada do 2


 tabuada do 3

The code that generates the output above is as follows:

void main()
{


int i,x, a, resultado;

printf(" Introduza o numero da tabuada que quer!\n\n");

scanf("%d", &a);

i = 0;

x = 0;

do  {
    printf(" tabuada do %d\n\n", x);

{

        while (i <= 10)
        {
            resultado = i*x;
            printf(" %dx%d=%d\n", i, a, resultado);
            i++;
        }
    }

        x++;

} while (x <= a);

}
    
asked by anonymous 17.08.2015 / 19:30

2 answers

6

You are not restarting the table counter every time you change numbers. I took the opportunity to give an organized one in the code. There are some conceptual errors about the table that I took to solve.

#include <stdio.h>

int main() {    
    int i = 1, x = 1, a, resultado;
    printf(" Introduza a quantidade de tabuadas que quer! ");
    scanf("%d", &a);

    do  {
        printf("\nTabuada do %d\n", x);
        while (i <= 10) {
            resultado = i*x;
            printf(" %dx%d=%d\n", i, a, resultado);
            i++;
        }
        i = 1; // <============ seu problema estava aqui.
        x++;
    } while (x <= a);
    return 0;
}

See running on ideone .

    
17.08.2015 / 19:38
4

Just reset the i counter, since it is the control of your table.

After the while switch to:

i = 0;

Looking like this:

void main()
{
int i,x, a, resultado;

printf(" Introduza o numero da tabuada que quer!\n\n");

scanf("%d", &a);

i = 0;

x = 0;

do  {
    printf(" tabuada do %d\n\n", x);
    {
        while (i <= 10)
        {
            resultado = i*x;
            printf(" %dx%d=%d\n", i, a, resultado);
            i++;
        }
        i = 0;
    }
    x++;
} while (x <= a);
}
    
17.08.2015 / 19:33