Problem to remove space after line break

0
#include <stdio.h>

int main (){

    int x,y,i;

    scanf("%d",&x);
    scanf("%d",&y);

    if ((x<y)&&(x>1)&&(x<20)&&(y<10000)){
        for (i=1;i<=x;i++){
            printf("%d ",i);
                }
            printf("\n");
       for (i=(x+1);i<=y;i++){
           printf("%d ",i);
       }
   }
 return 0;
}

program goal:

1st: The program reads two numbers X and Y (X minor Y). The following shows a sequence from 1 to Y, moving to the next line to each X numbers.

2nd: Each sequence should be printed on one line, with a space between each number.

3rd: The entry contains two integers X (greater than 1 and less than 20) and Y (greater than X and less than 100000) respectively. p>

4th: OBS: The program does not need to have an answer for the cases where the program does not run, nor does it need user interaction.

>

*** Example demonstrating the error of my program:

Removing this last space before the break of each line, the issue will be solved, I just can not find a way to do that.

    
asked by anonymous 24.01.2018 / 21:46

2 answers

0

Your program displays the 1 a X string, however, the question statement says nothing about displaying this string! Which one is correct?

To identify values of Y that are multiples of X just check if the rest of the division (or module ) of Y by X is equal to zero, see:

if( y % x == 0 )
    printf( "Y eh multiplo de X!\n" );

Following your reasoning and the statement of the question, follows a commented solution:

#include <stdio.h>

int main( void )
{
    int i,x,y;

    /* 1) O programa le dois numeros X e Y (X menor Y)... */
    printf("X: ");
    scanf( "%d", &x );

    printf("Y: ");
    scanf( "%d", &y );

    /*
        3) A entrada contem dois numeros inteiros X (maior que 1 e menor que 20) e
           Y (maior que X e menor que 100000) respectivamente.
    */
    if( (x<y) && (x>1) && (x < 20) && (y < 10000) )
    {
        /* 1) A seguir mostra uma sequencia de 1 a Y... */
        for ( i = 1; i <= y; i++ )
        {
            printf( "%d", i );

            /* 1) Passando para a proxima linha a cada X numeros... */
            /* ...e passando pra ultima linha na ultima iteracao    */
            if( (i % x == 0) || (i == y) )
                printf("\n");
            else
                printf(" "); /* Pulo do gato! */
        }
    }
    else
    {
        /* 4) OBS: O programa nao precisar ter resposta para os casos em que o programa nao roda */
        printf("Entrada invalida!\n");
    }

    return 0;
}

Test ( X=19 and Y=60 ):

X: 19
Y: 60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60

Test ( X=3 and Y=17 ):

X: 3
Y: 17
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17

Test ( X=4 and Y=15 ):

X: 4
Y: 15
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15

Test ( X=2 and Y=8 ):

X: 2
Y: 8
1 2
3 4
5 6
7 8
    
25.01.2018 / 13:03
1

Treat the first as an exception that does not have a space, the rest goes all within normal.

#include <stdio.h>

int main () {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    if (x < y && x > 1 && x < 20 && y < 10000) {
         printf("%d", 1);
        for (int i = 2; i <= x; i++) printf(" %d", i);
        printf("\n%d", x + 1);
        for (int i = x + 2; i <= y; i++) printf(" %d", i);
    }
}

You can do the reverse and treat the latter as an exception, but I do not like it.

    
24.01.2018 / 22:16