In the HSC examination, there are six subjects each having a maximum of 100 marks. To pass the examination, a student has to score at least 35 marks in each subject. If a student passes, total marks and percentage marks are calculated and a class is awarded based on the percentage marks (perce)as follows:
75 < perce < 100 First class with Distinction
60 < perce < 75 First class
50 < perce < 60 Second class
35 < perce < 50
Let us use a one-dimensional integer array marks having six elements to store the marks obtained by a student in six subjects, variables total of type int and perce of type float to store total marks and percentage marks, respectively, and variable result of type char to store the examination result. The strings representing the result and class obtained by a student are not stored in any variables. Instead, they are printed directly on the screen. The algorithm for this program is given below.
Read marks in six subjects
Determine result (‘P’ or ‘F’)
if (result== ‘P’) {
Calculate total and perce
Determine class
Print total,perce,result and class
else print result
We can determine the result and total marks using the statements given below.
if (marks[O] >= 35 && marks[l) >= 35 && marks[2) >= 35 && marks[3) >= 35 && marks[4] >= 35 && marks[5] >= 35)
result = ‘P’;
else result= ‘F’;
total= marks[O] + marks[l] + marks[2] + marks[3) + marks[4] + marks[S];
However, it is much more convenient to use the for loop to determine the result and total marks as shown in the program given below.
/* HSC marknlist program */
#include <stdio.h>
void main ()
{
int marks[6], total; /* marks in 6 subjects and total marks *!
float perce; /* percentage marks */
char result; /* char indicating result (Pass/Fail) */
int i;
clrscr();
/* accept marks */
printf(“Enter marks in six subjects: “);
for (i = 0; i < 6; i++)
scanf(“%d”, &marks[i]);
/* determine result */
result= ‘P’; /*assume student has passed*/
for (i = 0; i < 6; i++)
{
if (marks[i] < 35)
{
result = ‘F’;
break;
}
}
if (result == ‘P’)
{
/* Student has passed: calc total marks and percentage marks */
total = 0;
for (i = 0; i < 6; i++)
total+= marks[i];
float perce = total / 6.0;
/* print result, total marks and percentage marks */
printf (“Result: Pass\n”);
printf(“Total marks: %d\n”, total);
printf(“Percentage marks: %5.2f\n”, perce);
/* determine and print class */
if (perce >= 75.0)
printf(“Class: First with distinction\n”);
else if (perce >= 60.0)
printf(“Class: First\n”);
else if (perce >= 50.0)
printf(“Class: Second\n”);
else printf(“Class: —\n”);
}
else printf(“Result: Fail\n”);
getch();
}
Enter marks in six subjects: 40 50 60 70 80 90
Result: Pass
Total marks: 390
Percentage marks: 65.00
Class: First
Enter marks in six subjects: 40 50 60 70 80 30
Result: Fail