How do I stop organizing tables in while?

Asked

Viewed 2,293 times

2

How do I stop organizing this table ? Sum, subtraction and multiplication respectively ?

#include <stdio.h>

int main (){
int i=0, num=0;

printf ("Digite um n£mero: ");
scanf ("%d",&num);

printf ("\n");

while (i<=9){
    i++;
    printf ("%d + %d = %d\n", i, num, num+i);

    printf ("%d - %d = %d\n", i, num, i-num);

    printf ("%d X %d = %d\n", i, num, num*i);
}
}
  • how to organize?

  • When I run it gets all messy like 1+1, 1-1, 1x1, 2+1, 2+1, 2-1, 2x1 ... and so on to 10.

1 answer

7


Just replace the ' n’s of the first and second printf with ' t’s, in this way it will not break the line, which in case is ' n', and will still give a tab, in case t, (equivalent to four spaces).

#include <stdio.h>

int main ()
{
    int i=0, num=0;

    printf ("Digite um n£mero: ");
    scanf ("%d",&num);

    printf ("\n");

    while ( i <= 9 )
    {
        i++;
        printf ("%d + %d = %d\t", i, num, num+i);

        printf ("%d - %d = %d\t", i, num, i-num);

        printf ("%d X %d = %d\n", i, num, num*i);
    }

    return 0;
}

In this case the output will be like this: Neste caso o output ficara assim:

another way:

#include <stdio.h>
#include <locale.h>

int main ()
{
    int i = 0, num = 0;

    setlocale(LC_ALL, "");

    printf ("Digite um número: ");
    scanf (" %d", &num);

    printf ("\nAdição:\n");
    for ( i = 0; i <= 9; i++ )
        printf ("%d + %d = %d\n", i, num, num+i);

    printf ("\nSubtração:\n");
    for ( i = 0; i <= 9; i++ )
        printf ("%d - %d = %d\n", i, num, i-num);

    printf ("\nMultiplicação\n");
    for ( i = 0; i <= 9; i++ )
        printf ("%d X %d = %d\n", i, num, num*i);

    return 0;
}

This way the output will be sequential, I added the locale library. h to use accent, the command setlocale(LC_ALL, ""); is the command that allows BR accents to be used, the image of this output is this: inserir a descrição da imagem aqui

  • It worked very well not 1+1, 1-1, 1x1, 2+1, 2+1, 2-1, 2x1 ... so q with spaces, I want it to be something like 1+up to 10, 1-up to 10, 1xate to 10

  • guy edited the answer, hope to have helped, otherwise explain a little better what you want, not very clear, make a correct example of output bag, this will help.

  • I saw now the edition, it was right now <3 Brigadão

Browser other questions tagged

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