I created a C program for a question and I am facing some problem given below. I have used logical and conditional operators in this program.
//Question - c4.d.f
/*
A certain grade of steel is graded according to the following conditions:
(i)Hardness must be greater than 50
(ii)Carbon content must be less than 0.7
(iii)Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade 8 if conditions (ii) and (iii) are met
Grade 7 if conditions (i) and (iii) are met
Grade 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness,
carbon content, and tensile strength of the steel under consideration and
output the grade of the steel.
*/
#include<stdio.h>
int main()
{
int hrd,tslstr;
float crbct;
int con1, con2, con3;
printf("Please enter the values of the Hardness, Carbon content, Tensile Strength\n");
scanf("%d%f%d",&hrd,&crbct,&tslstr);
//conditions
hrd>50?con1=1:(con1=0);
crbct<0.7?con2=1:(con2=0);
tslstr>5600?con3=1:(con3=0);
//Calculating Grades
if( con1 && con2 && con3)
printf("Grade 10\n");
else if( con1 && con2 )
printf("Grade 9\n");
else if( con2 && con3 )
printf("Grade 8\n");
else if( con1 && con3 )
printf("Grade 7\n");
else if( con1 || con2 || con3 )
printf("Grade 6\n");
else
printf("Grade 5\n");
return 0;
}
I run the program and give the values (given below) , so I get "Grade 5" printed but it's printing "Grade 6". Please help me to find the problem in this code.
Please enter the values of the Hardness, Carbon content, Tensile Strength
50
0.7
5600
Grade 6