Display the largest and smallest values between two integers when they are different from each other

3

I can not display the lower and upper values.

  

Make an algorithm that reads two numbers and indicates whether they are the same or, if different, show 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");
    
asked by anonymous 14.09.2018 / 14:33

2 answers

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've used a chained decision structure , that is, the program will only enter if or else if the values are different.

    
19.09.2018 / 14:27
5

You started well using else in the first if , should have continued like this. So when v1 is greater than v2 you already have reason to write which is the largest and which is the lowest at the same time. And if it is not, there it writes the same but this time with the inverted variables.

#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);
}

See running on ideone . And in Coding Ground . Also I put GitHub for future reference .

Or you might even want to have two if s, you should use if , else if , and else . It's up to you, but I think it's a worse solution.

    
14.09.2018 / 14:38