ola quero fazer um programa, em c, que devolve o minimo multiplo comum entre dois numeros

Asked

Viewed 36 times

0

This is the code I have but it’s not returning anything. what will be wrong?

#include <stdio.h>
#define nao 0
#define ok 1
int main(){
    int x,y,contador1=1, contador2=1,maior,menor, mmc,estado=nao;
    scanf("%d %d",&x,&y);
    if (x<y){
        maior=y;
        menor =x;
    }
    else{
        maior=x;
        menor=y;
    }

    if(x>0 && y>0){
        while(estado==nao){
            while (contador1<=10){
                if (menor*contador1==maior*contador2){
                    estado=ok;
                    mmc=menor*contador1;
                }
                else{
                    contador1++;
                    
                }
            }
            contador2++;    
        } 
    }

    printf("%d\n",mmc);
    return 0;
}

thank you!

  • 1

    What happens if contador1 become greater than 10 and estado is still equal to nao?

  • Counter1 doesn’t get bigger than 10 because the while only goes up to 10

  • You can reach up to 11 in your code. If it is equal to 10 still enters the loop and is incremented by 11. Take a table test to better understand the code you wrote.

  • but my question is why the code does not return anything.

  • As soon as you can answer what happens in your code when contador1 is equal to 11 and estado is equal to nao You’ll understand why you don’t return anything. If you don’t understand this situation well, it means you didn’t understand 100% of the code itself, so I suggested doing the table test. You can read more about at What is a Table Test? How to Apply It?.

  • @user227365 with what values of x and y have you tested? Your program comes to end by entering these values?

Show 1 more comment
No answers

Browser other questions tagged

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