I’m not displaying the multiple correctly

Asked

Viewed 82 times

0

I’ve developed an algorithm that should display the largest multiple between num1 <= num2. I used as example 7 and 50 and worked correctly, displaying the value 49, but when sending for evaluation there is an inconsistency that I do not recognize.

#include <stdio.h>

int main() {
  int num1, num2, i, temp, resultado;

  scanf("%i", & num1);
  scanf("%i", & num2);

  if (num1 > num2) {
    temp = num1;
    num1 = num2;
    num2 = temp;
  }

  for (i = num1; i <= num2; i++) {
    if (num1 % i == 0) {
      resultado = num1 * i;
    }
  }
  if (resultado <= num2) {
    printf("%i\n", resultado);
  } else {
    printf("sem multiplos menores que %i", num2);
  }
  return 0;
}
  • "but when sending for evaluation there is an inconsistency that I am not recognizing" as well as inconsistency, which means?

  • Hello, I’ll show you the link to the question, http://thehuxley.com/problem/36. The tips are: Are you really finding the biggest one? Isn’t there another even bigger number? .

  • 1

    need only explain what you mean by inconsistency

  • The site states that I am not finding the largest multiple nor including the num2

  • @Mauroalmeida The example given on the site with the input and output are these, http://thehuxley.com/problem/36

  • On the understanding of the question: in my opinion the question does not ask you to invert the numbers if num2 < num1, in this case the answer would be "without multiples smaller than N". In terms of logic: as it is to find the largest multiple I would start the test on N and would decrement until finding the first multiple, or eventually get to M, in a while loop.

Show 1 more comment

3 answers

2


The statement is not so clear, so I wouldn’t worry too much if this is right, just focus on the result, which would even give to cheat since you can’t even know exactly what the challenge calls for ambiguity of the text. Nothing there asks to make the limit always the highest value typed and I think this is an algorithm error, but I left it so. And maybe what he wants is the square of the smallest number, because then it would make sense not to have a valid value, if it’s a simple multiple so any smaller number is valid, it just can’t be equal, unless the statement means that it has to be a number that has been multiplied by at least 2, anyway, let’s pretend that this only to give the result (I would skip poorly done exercise or cheating only giving 49 as a result). To calculate multiple, just use mathematics and do not need complications as it was in the algorithm. Think everything mathematically, it’s much simpler, only when mathematics doesn’t solve should you think about a complex algorithm.

#include <stdio.h>

int main() {
    int num1, num2;
    scanf("%d", &num1);
    scanf("%d", &num2);
    if (num1 > num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }
    int multiplo = (num2 / num1) * num1;
    if (multiplo <= num2) printf("%d\n", multiplo);
    else printf("sem multiplos menores que %d", num2);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • But isn’t the goal to show the largest multiple of the first number that is less than the second number ? This is what I read in the statement, and it makes sense for the example shown, that is, the largest multiple of 7 that is less than or equal to 50, so it results in 49.

  • As I said, the statement is ambiguous. Even if it were different you could do it with math without ties, of course it depends on how different it was, but then it would be too different.

1

Mano na real the statement ta claro has 2 int the first is the number that you need to find the multiple, the second is the bounder of the multiple, that is you have to find the largest multiple and it has to be less than or equal to the number 2

then you don’t need to detain the greatest number

    if (num1 > num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }

don’t need it, don’t ask you to find the biggest one between the two

a solution would be:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int num, less_than, multiple, i;
    scanf("%d%d", &num, &less_than);

    multiple = 0;
    i = 0;
    for (i = less_than; i >= 0; i--) {
        if ((i % num) == 0) {
            multiple = i;
            break;
        }
    }

    if (multiple > 0) {
        printf("%d", multiple);
    } else {
        printf("sem multiplos menores que %i", less_than);
    }
    return 0;
}

0

He finds the value 49when num1 == 1, I mean, the first iteration. On the website you referenced: THEHUXLEY does not require that it be necessary num1 <= num2, as @Adir Kuhn pointed out

#include <stdio.h>

int main(void) {
  int num1, num2, maior = -1;

  scanf("%d", &num1);
  scanf("%d", &num2);
  for(int i=1; i*num1 <= num2; i++){
     maior=i*num1;
  }
  if (maior != -1) {
    printf("%d\n", maior);
  } else {
    printf("sem multiplos menores que %d", num2);
  }
  return 0;
}

This is more a matter of algorithm than C, because in C we could obtain the value using the fact that the division of integers results in an integer, and we could use, after verifying that 0 < num1 < num2 :

printf("Numero: %d.\n", (num2/num1) * num1);

Therefore, they are not equivalent (num2/num1) * num1 and num2.

Browser other questions tagged

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