PHP Program that calculates the division obtained by a student based on their marks in five subjects:

The marks obtained by a student in 5 different subjects are input through the keyboard. The students gets a division as per the following rules: Percentage above or equal to 60- First division . Percentage between 50 and 59- Second division. Percentage between 40 and 49- Third division. percentage less than 40- Fail. Write a program to calculate the division obtained by the student using else if condition.

 // Get marks from user input

$subject1 = $_POST['subject1'];

$subject2 = $_POST['subject2'];

$subject3 = $_POST['subject3'];

$subject4 = $_POST['subject4'];

$subject5 = $_POST['subject5'];


// Calculate total marks and percentage

$totalMarks = $subject1 + $subject2 + $subject3 + $subject4 + $subject5;

$percentage = ($totalMarks / 500) * 100;


// Determine division based on percentage

if ($percentage >= 60) {

    $division = "First";

} elseif ($percentage >= 50 && $percentage <= 59) {

    $division = "Second";

} elseif ($percentage >= 40 && $percentage <= 49) {

    $division = "Third";

} else {

    $division = "Fail";

}


// Output division to user

echo "Total marks obtained: $totalMarks<br>";

echo "Percentage obtained: $percentage%<br>";

echo "Division obtained: $division";

In this example, we first get the marks for each subject from user input using the $_POST superglobal. We then calculate the total marks and percentage by adding up the subject marks and dividing by the total possible marks (in this case, 500).

We use an if-elseif-else statement to determine the division based on the percentage. If the percentage is greater than or equal to 60, the student gets a first division. If the percentage is between 50 and 59, they get a second division. If the percentage is between 40 and 49, they get a third division. Otherwise, they fail.

Finally, we output the total marks, percentage, and division to the user using the echo statement. Note that this is just a simple example and in real-world scenarios, it's important to validate user input and handle any potential errors that may occur during the calculation.

Post a Comment

0 Comments