Display the highest and lowest value between two integers when they are different from each other

Asked

Viewed 2,069 times

3

I am not able to display the values of smaller and larger.

Make an algorithm that reads two numbers and indicates whether they are equal or, if different, shows the largest and smallest in this sequence.

int V1,V2;

printf("Valor 1 :"); scanf("%d",&V1);
printf("Valor 2 :"); scanf("%d",&V2);

if (V1==V2){
    printf("O valor é igual \n\n ");
}
else{
    printf("O valor é diferente \n\n");
}
if (V1>V2){
    printf("O valor maior é %d",V1);
}
if (V1<V2){
    printf("O valor menor é %d", V2);
}
return(0);
system("pause");
  • 2

    Last if has to show the V1 and not V2, but I suggest you elaborate the question better with what you wanted to show and what you’re showing at the moment

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

5

You started out well using the else in the first if, should have stayed that way. So when v1 is greater than v2 already has reason to write which is bigger and which is smaller at the same time. And if not, then write the same, but this time with the variables reversed.

#include <stdio.h>

int main(void) {
    int v1, v2;
    printf("Valor 1 :"); scanf("%d", &v1);
    printf("Valor 2 :"); scanf("%d", &v2);
    if (v1 == v2) printf("O valor é igual\n");
    else printf("O valor é diferente\n");
    if (v1 > v2) printf("O valor maior é %d\nO valor menor é %d", v1, v2);
    else printf("O valor maior é %d\nO valor menor é %d", v2, v1);
}

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

Or maybe I wish I didn’t have two ifs, then you should use a if, one else if and a else. Stay and do it, but I find a worse solution.

  • Thanks, I’m starting in the area.

  • @Gabriel see in the [tour] the best way to say thank you

3


#include <stdio.h>

int main()
{
    int v1, v2;
    printf("Valor 1:");
    scanf("%d", &v1);
    printf("Valor 2:");
    scanf("%d", &v2);

    if(v1 == v2){
        printf("Os valores %d e %d são iguais.", v1, v2);
    }   
    else {
        printf("Os valores %d e %d são diferentes.", v1, v2);
        if(v1 > v2){
            printf("\nO maior valor é: %d e o menor é: %d.", v1, v2);
        }
        else{
            printf("\nO maior valor é: %d e o menor é: %d.", v2, v1);
        }
    }
}

You just need to know if a value is higher or lower if they are different, so I used a linked decision-making structure, that is, the program will only enter the if or in the else if the values are different.

Browser other questions tagged

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