Write a program to display grade of student as per % inputed
Answers
Answer:
Given an integer array marks, which comprises of marks scored by a student (out of 100) in different subjects, the task is to assign a grade to the student. The grade is found out by taking the percentage of the marks scored by the student. The percentage is calculated as:
The grade is assigned using the following rules:
PERCENTAGE GRADE
90 and above A
80 to 89 B
60 to 79 C
33 – 59 D
below 33 F
Examples:
Input: marks = { 25, 65, 46, 98, 78, 65 }
Output: C
Input: marks = { 95, 88, 98, 93, 92, 96 }
Output: A
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach:
Initialize a variable to sum all the marks scored by the student, total to 0.
Initialize a variable to store the grade of the student, grade to ‘F’.
First, we iterate through the marks array and find the total marks scored by the student.
Then, we apply the formula described above to calculate the percentage.
We then make a nested if else construct to assign proper grade to the student.
For more on decision making and different types of decision making constructs, refer Decision Making in Java.
Below is the implementation of the above approach:
filter_none
edit
play_arrow
brightness_4
// CPP program to assign grades to a student
// using nested if-else
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Store marks of all the subjects in an array
int marks[] = { 25, 65, 46, 98, 78, 65 };
// Max marks will be 100 * number of subjects
int len = sizeof(marks) / sizeof(marks[0]);
int max_marks = len * 100;
// Initialize student's total marks to 0
int total = 0;
// Initialize student's grade marks to F
char grade = 'F';
// Traverse though the marks array to find the sum.
for (int i = 0; i < len; i++)
{
total += marks[i];
}
// Calculate the percentage.
// Since all the marks are integer,
// typecast the calculation to double.
double percentage = ((double)(total) / max_marks) * 100;
// Nested if else
if (percentage >= 90)
{
grade = 'A';
}
else
{
if (percentage >= 80 && percentage <= 89)
{
grade = 'B';
}
else
{
if (percentage >= 60 && percentage <= 79)
{
grade = 'C';
}
else
{
if (percentage >= 33 && percentage <= 59)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
}
}
cout << (grade) << endl;;
}
// This code is contributed by
// Surendra_Gangwar
Output:
C
Explanation: