Problem in table in C

2

Can anyone tell me why the looping is not working correctly?

It was to print the table from 0 to 10, but instead it is printing numbered * 11.

Follow the code below:

#include <stdio.h>
int main(void)

{
int numero, cont=0;

    printf("Digite um numero: ");
    scanf("%d",&numero);

    for (cont=0; cont<=10 ; cont++);
        {
        printf("%d x %d = %d \n",numero,cont,numero*cont);
        }
return 0;
}

    
asked by anonymous 23.03.2018 / 20:22

2 answers

4

The way that printf is displayed only once after the counter exits the loop, ie when the count has a value of 11. To correct it, just remove the ; ; > that is after for() .

Here's how it should look:

#include <stdio.h>
int main(void)

{
int numero, cont=0;

    printf("Digite um numero: ");
    scanf("%d",&numero);

    for (cont=0; cont<=10 ; cont++){
        printf("%d x %d = %d \n",numero,cont,numero*cont);
    }
    return 0;
}

    
23.03.2018 / 20:27
2

I had ; after for , so he would not execute anything and jump straight to:

printf("%d x %d = %d \n",numero,cont,numero*cont); 

Running once. Here is corrected code.

#include <stdio.h>
int main(void)

{
int numero, cont=0;

    printf("Digite um numero: ");
    scanf("%d",&numero);

    for (cont=0; cont<=10 ; cont++)
        {
        printf("%d x %d = %d \n",numero,cont,numero*cont);
        }
return 0;
}
    
23.03.2018 / 20:28