Tabulated problem in C

Asked

Viewed 247 times

2

Can anyone tell me why the looping isn’t working properly?

It was to print the 0 to 10 times, but instead it is printing typed number * 11.

Follow the 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;
}

inserir a descrição da imagem aqui

  • 2

    takes the ; front of the for

2 answers

4


The way the printf is only displayed once after the counter exits the repeat loop, that is, when the cont has a value equal to 11. To correct just remove the ; who is after the for().

It goes on as it should:

#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;
}

inserir a descrição da imagem aqui

2

Had a ; after the for, soon he did nothing and jumped straight to:

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

Running it one time. Follows 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.