jeudi 26 mars 2020

How od I solve this middle number problem?

Write a program where the user inputs 3 float numbers and the program checks which is medium size number. Example : a = 1.5, b = 7.8, and c = 3.0 and output should be c. This is what I've tried and it worked for one case, but I'm still doing too much spaghetti code and I'm still learning how to write code efficiently. My code:

#include <stdio.h>

int main(){
    float a, b, c;

    scanf("%f %f %f", &a, &b, &c);

    if(a < b && c < a)
        printf("%.1f", a);
    else if(b < a && b > c)
        printf("%.1f", b);
    else if(c > a && c < b)
        printf("%.1f", c);
    else
    {
        printf("not good"); //I wrote this part to check if the code is good
    }




    return 0;
}

I'm still trying to get the hang of the if loops and I was just confused with this problem. Do you have any suggestions?

Aucun commentaire:

Enregistrer un commentaire