PHP Script to Calculate Total Marks of Student and Display Grade
PHP script to calculate total marks of student and display grade.
In this article, you will learn how to write PHP script to calculate total marks of student and display grade.
Example
The Average marks = 74.2
The Percentage = 74.2%
The Grade = 'C'
Standard Formula
Total = p + c + m + e + h
Average = total / 5.0
Percentage = (total / 500.0) * 100
In this article, we solve this problem in three methods:
PHP Program to Find Grade of a Student using If-else
<?php
// PHP Script to Calculate Total Marks of Student and Display Grade
// These are the marks of five subjects
$sub_1 = 95;
$sub_2 = 85;
$sub_3 = 74;
$sub_4 = 64;
$sub_5 = 53;
$total = NULL;
$average = NULL;
$percentage = NULL;
$grade = NULL;
// It will calculate total, average, percentage, and grade
$total = $sub_1 + $sub_2 + $sub_3 + $sub_4 + $sub_5;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;
if ($average >= 90)
$grade = "A";
else if ($average >= 80 && $average < 90)
$grade = "B";
else if ($average >= 70 && $average < 80)
$grade = "C";
else if ($average >= 60 && $average < 70)
$grade = "D";
else
$grade = "E";
// It will print the final output
echo "The Total marks = " . $total . "/500\n";
echo "The Average marks = " . $average . "\n";
echo "The Percentage = " . $percentage . "%\n";
echo "The Grade = '" . $grade . "'\n";
?>
Output
The Total marks = 371/500
The Average marks = 74.2
The Percentage = 74.2%
The Grade = 'C'
Explanation
Average: we take the marks of five subjects as input after that the sum of these marks is divided by 5
then It will return the average value of the marks.
Percentage: we made a division of the sum of user's marks by 500
then It multiplied by 100
then It will return the percentage value of the marks.
Grade: we compared the value of the Average marks with this range of marks to find the Grade of students like:
If Average marks >= 90
then Its Grade is 'A'
If Average marks >= 80 and < 90
then Its Grade is 'B'
If Average marks >= 70 and < 80
then Its Grade is 'C'
If Average marks >= 60 and < 70
then Its Grade is 'D'
If Average marks < 60
then Its Grade is 'E'
PHP Program to Find Grade of a Student using Switch Case
<?php
// PHP Program to Find Grade of a Student using Switch Case
// These are the marks of five subjects
$sub_1 = 95;
$sub_2 = 85;
$sub_3 = 74;
$sub_4 = 64;
$sub_5 = 53;
// It will calculate total, average, percentage, and grade
$total = $sub_1 + $sub_2 + $sub_3 + $sub_4 + $sub_5;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;
// It will divides average by 10 check switch expression
$sAvg = (int) ($average / 10);
// It will calculate the Grade
switch ($sAvg) {
case 10:
$grade = 'A';
break;
case 9:
$grade = 'A';
break;
case 8:
$grade = 'B';
break;
case 7:
$grade = 'C';
break;
case 6:
$grade = 'D';
break;
default:
$grade = 'E';
break;
}
// It will print the final output
echo "The Total marks = " . $total . "/500\n";
echo "The Average marks = " . $average . "\n";
echo "The Percentage = " . $percentage . "%\n";
echo "The Grade = '" . $grade . "'\n";
?>
PHP Program to Find Grade of a Student using Nested If-else
<?php
// PHP Program to Find Grade of a Student using Nested If-else
// These are the marks of five subjects
$sub_1 = 85;
$sub_2 = 54;
$sub_3 = 74;
$sub_4 = 49;
$sub_5 = 38;
// It will calculate total, average, percentage, and grade
$total = $sub_1 + $sub_2 + $sub_3 + $sub_4 + $sub_5;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;
if ($average >= 90)
$grade = "A";
else {
if ($average >= 80 && $average < 90)
$grade = "B";
else {
if ($average >= 70 && $average < 80)
$grade = "C";
else {
if ($average >= 60 && $average < 70)
$grade = "D";
else
$grade = "E";
}
}
}
// It will print the final output
echo "The Total marks = " . $total . "/500\n";
echo "The Average marks = " . $average . "\n";
echo "The Percentage = " . $percentage . "%\n";
echo "The Grade = '" . $grade . "'\n";
?>
Output
The Total marks = 300/500
The Average marks = 60
The Percentage = 60%
The Grade = 'D'
Also, visit these links
C Program to Calculate Percentage and Grade
C++ Program to Calculate Sum of 5 Subjects and Find Percentage and Grade
Java Program to Enter Marks of Five Subjects and Calculate Total, Percentage, and Grade
Python Program to Calculate Total Marks Percentage and Grade of a Student