vendredi 9 octobre 2015

if else statement not giving expected answer

the values entered are not matching with the output grade as it given 10 grade even condition for 10 are not met whats the error in this its about giving a certain grade to steel depending on the conditions also how can we modify this code?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main() {
// program grade the steel on quality basis

int hardness;
int strength;
float carbon;



printf("Enter the hardness of steel:");     // condition 1  hardness should be >= 50
scanf("%d", &hardness);

printf("Enter the tensile strength:");       // condition 2   strength should be >= 5600
scanf("%d", &strength);


printf("Enter carbon content:");              //condition 3      carbon less then 0.7
scanf("%.2f", &carbon);



if ((hardness >= 50) && (carbon < 0.7) && (strength >= 5600)) {        // all true
    printf("\ngrade = 10");


}

else if ((hardness >= 50) && (carbon < 0.7) && (strength <= 5600)) {     // 1 and 2 true
    printf("\ngrade = 9");



}
else if ((hardness <= 50) && (carbon < 0.7) && (strength >= 5600)) {    // 2 and 3 true
    printf("\ngrade = 8");
}

else if ((hardness >= 50) && (carbon > 0.7) && (strength >= 5600)) {    // 1 and 3 true
    printf("\ngrade = 7");
}


 else if ((hardness >= 50) && (carbon > 0.7) && (strength <= 5600)) {    // any one true
    printf("\ngrade = 6");
}

 else if ((hardness <= 50) && (carbon < 0.7) && (strength <= 5600)) {    // any one true
    printf("\ngrade = 6");

    else if ((hardness <= 50) && (carbon < 0.7) && (strength >= 5600)) {    // any one true
    printf("\ngrade = 6");
}

else   {
    printf("\ngrade = 5");                     // none true

}


_getch();
return 0;

}

Aucun commentaire:

Enregistrer un commentaire