Write a Program That Reads Student Scores Gets the Best Score and Then Assigns Grades
Program to Assign grades to a student using Nested If Else
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:
Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out ourself-paced courses designed for students of grades I-XII.
Start with topics likePython, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, becauseGeeksforGeeks School is now here!
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
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:
C++
#include<bits/stdc++.h>
using
namespace
std;
int
main()
{
int
marks[] = { 25, 65, 46, 98, 78, 65 };
int
len =
sizeof
(marks) /
sizeof
(marks[0]);
int
max_marks = len * 100;
int
total = 0;
char
grade =
'F'
;
for
(
int
i = 0; i < len; i++)
{
total += marks[i];
}
double
percentage = ((
double
)(total) / max_marks) * 100;
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;;
}
Java
class
GFG {
public
static
void
main(String args[])
{
int
marks[] = {
25
,
65
,
46
,
98
,
78
,
65
};
int
max_marks = marks.length *
100
;
int
total =
0
;
char
grade =
'F'
;
for
(
int
i =
0
; i < marks.length; i++) {
total += marks[i];
}
double
percentage
= ((
double
)(total) / max_marks) *
100
;
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'
;
}
}
}
}
System.out.println(grade);
}
}
Python3
if
__name__
=
=
"__main__"
:
marks
=
[
25
,
65
,
46
,
98
,
78
,
65
]
max_marks
=
len
(marks)
*
100
total
=
0
grade
=
'F'
for
i
in
range
(
len
(marks)):
total
+
=
marks[i]
percentage
=
((total)
/
max_marks)
*
100
if
(percentage >
=
90
):
grade
=
'A'
else
:
if
(percentage >
=
80
and
percentage <
=
89
) :
grade
=
'B'
else
:
if
(percentage >
=
60
and
percentage <
=
79
) :
grade
=
'C'
else
:
if
(percentage >
=
33
and
percentage <
=
59
) :
grade
=
'D'
else
:
grade
=
'F'
print
(grade)
C#
using
System;
class
GFG
{
public
static
void
Main()
{
int
[]marks = { 25, 65, 46, 98, 78, 65 };
int
max_marks = marks.Length * 100;
int
total = 0;
char
grade =
'F'
;
for
(
int
i = 0; i < marks.Length; i++)
{
total += marks[i];
}
double
percentage = ((
double
)(total) /
max_marks) * 100;
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'
;
}
}
}
}
Console.WriteLine(grade);
}
}
PHP
<?php
$marks
=
array
(25, 65, 46, 98, 78, 65);
$max_marks
= sizeof(
$marks
) * 100;
$total
= 0;
$grade
=
'F'
;
for
(
$i
= 0;
$i
< sizeof(
$marks
);
$i
++)
{
$total
+=
$marks
[
$i
];
}
$percentage
= ((
$total
) /
$max_marks
) * 100;
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'
;
}
}
}
}
echo
$grade
.
"\n"
;
?>
Javascript
<script>
let marks=[25, 65, 46, 98, 78, 65];
let max_marks = marks.length * 100;
let total = 0;
let grade =
'F'
;
for
(let i = 0; i < marks.length; i++) {
total += marks[i];
}
let percentage
= ((total) / max_marks) * 100;
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'
;
}
}
}
}
document.write(grade);
</script>
Write a Program That Reads Student Scores Gets the Best Score and Then Assigns Grades
Source: https://www.geeksforgeeks.org/program-to-assign-grades-to-a-student-using-nested-if-else/
0 Response to "Write a Program That Reads Student Scores Gets the Best Score and Then Assigns Grades"
Post a Comment