Dimensions of the puzzle

3

Link to the exercise in question

Example entry

1000

2000

500

Sample output

40 25

50 40

25 20

The code works correctly, but when it comes to telling you that the puzzle has 64 pieces, it takes me out 9 8, should not I get 8 8?

#include <stdio.h>

main()
{
    int n, i, altura;

    printf("Introduce numero de piezas: ");
    scanf("%d", &n);

    i=1;
    do
    {
        altura=n/i;
        i++;
    }while(n%altura!=0 || i<=altura);

    printf("%d %d", i, altura);
}
    
asked by 22.12.2017 в 17:47
source

1 answer

2

The code is correct; the only detail that you missed is that ...

do {
  altura = n / i;
  i++; // <-- SIEMPRE lo incrementas
} while( n % altura != 0 || i <= altura );

Always you increase i , whatever the result of altura = n / i .

The simplest solution ? Keep it in mind when showing the result; -)

printf( "%d %d", i - 1 , altura );
    
answered by 22.12.2017 в 17:53