In: Computer Science
Write a program to take input of scores for Chemistry, Biology and English for a student and display the average with 2 decimal places if all of the three scores are in the range between 1 and 100 inclusive. Program should also display the course name for which scores are entered out of range telling Average couldn't be calculated.
Following is a sample run:
Enter scores for
Chemistry, Biology and English between 1 and 100:
45
89
96
Avg score is 76.67
Following is another sample run:
Enter scores for
Chemistry, Biology and English between 1 and 100:
900
78
89
Average cannot be calculated because Chemistry Scores were not in
range.
Following is another sample run:
Enter scores for
Chemistry, Biology and English between 1 and 100:
89
767
8787
Average cannot be calculated because Biology Scores were not in
range.
Average cannot be calculated because English Scores were not in
range.
Following is another sample run:
Enter scores for
Chemistry, Biology and English between 1 and 100:
87
90
-1
Average cannot be calculated because English Scores were not in
range.
Required program in C -->
#include<stdio.h>
int main(){
int chemistry, biology, english; //declare variables
chemistry,biology, and english as integer
float average; //declare variables average as float
printf("Enter chemistry marks");
scanf("%d",&chemistry); //read chemistry marks
printf("Enter biology marks");
scanf("%d",&biology); //read biology marks
printf("Enter english marks");
scanf("%d",&english); //read english marks
if(chemistry>=0&& chemistry<=100
&&biology>=0 && biology<=100 &&
english>=0 && english<=100){
average=(chemistry+biology+english)/3; //if in range, then
calculate average
printf("%.2f",average); // print average with 2 decimal
points
}
else{
if(chemistry<0 || chemistry>100){ //if chemistry not in
range, print chemistry not in range
printf("Average cannot be calculated because chemistry Scores were
not in range.");
}
if(biology<0 || biology>100){ //if biology not in range,
print biology not in range
printf("Average cannot be calculated because Biology Scores were
not in range.");
}
if(english<0 || english>100){ //if english not in
range, print english not in range
printf("Average cannot be calculated because English Scores were
not in range.");
}
}
}