Programa para asignar calificaciones a un estudiante usando Nested If Else

Dada una array de números enteros , que comprende las calificaciones obtenidas por un estudiante (de 100) en diferentes materias, la tarea es asignar una calificación al estudiante. La nota se obtiene tomando el porcentaje de las notas obtenidas por el alumno. El porcentaje se calcula como: 

La calificación se asigna utilizando las siguientes reglas:
 

Porcentaje Calificación
90 y más A
80 a 89 B
60 a 79 C
33 – 59 D
por debajo de 33 F

Ejemplos: 
 

Entrada: puntos = { 25, 65, 46, 98, 78, 65 } 
Salida: C
Entrada: puntos = { 95, 88, 98, 93, 92, 96 } 
Salida:
 

Acercarse: 
 

  • Inicializar una variable para sumar todas las notas obtenidas por el estudiante, total a 0. 
     
  • Inicialice una variable para almacenar la calificación del estudiante, calificación ‘F’. 
     
  • Primero, iteramos a través de la array de calificaciones y encontramos las calificaciones totales obtenidas por el estudiante. 
     
  • Luego, aplicamos la fórmula descrita anteriormente para calcular el porcentaje. 
     
  • Luego hacemos una construcción anidada if else para asignar la calificación adecuada al estudiante. 
     

Para obtener más información sobre la toma de decisiones y los diferentes tipos de construcciones de toma de decisiones, consulte Toma de decisiones en Java .
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// 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

Java

// Java program to assign grades to a student
// using nested if-else
 
class GFG {
    public static void main(String args[])
    {
 
        // 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 max_marks = marks.length * 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 < marks.length; 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';
                    }
                }
            }
        }
 
        System.out.println(grade);
    }
}

Python3

# Python3 program to assign grades
# to a student using nested if-else
 
if __name__ == "__main__":
 
    # Store marks of all the subjects
    # in an array
    marks = [25, 65, 46, 98, 78, 65 ]
 
    # Max marks will be 100 * number
    # of subjects
    max_marks = len(marks)* 100
 
    # Initialize student's total
    # marks to 0
    total = 0
 
    # Initialize student's grade
    # marks to F
    grade = 'F'
 
    # Traverse though the marks array
    # to find the sum.
    for i in range(len(marks)):
        total += marks[i]
 
    # Calculate the percentage.
    # Since all the marks are integer,
    percentage = ((total) /max_marks) * 100
 
    # Nested if else
    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)
 
# This code is contributed by ita_c

C#

// C# program to assign grades to a student
// using nested if-else
using System;
 
class GFG
{
public static void 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 max_marks = marks.Length * 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 < marks.Length; 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';
                }
            }
        }
    }
 
    Console.WriteLine(grade);
}
}
 
// This code is contributed by Ryuga

PHP

<?php
// PHP program to assign grades to a student
// using nested if-else
 
// Store marks of all the subjects in an array
$marks = array(25, 65, 46, 98, 78, 65);
 
// Max marks will be 100 * number of subjects
$max_marks = sizeof($marks) * 100;
 
// Initialize student's total marks to 0
$total = 0;
 
// Initialize student's grade marks to F
$grade = 'F';
 
// Traverse though the marks array to find the sum.
for ($i = 0; $i < sizeof($marks); $i++)
{
    $total += $marks[$i];
}
 
// Calculate the percentage.
// Since all the marks are integer,
// typecast the calculation to double.
$percentage = (($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';
            }
        }
    }
}
 
echo $grade . "\n";
 
// This code is contributed by Akanksha Rai
?>

Javascript

<script>
 
// Javascript program to assign grades to a student
// using nested if-else
 
// Store marks of all the subjects in an array
let marks=[25, 65, 46, 98, 78, 65];
 
// Max marks will be 100 * number of subjects
let max_marks = marks.length * 100;
 
 // Initialize student's total marks to 0
let total = 0;
 
// Initialize student's grade marks to F
        let grade = 'F';
   
        // Traverse though the marks array to find the sum.
        for (let i = 0; i < marks.length; i++) {
            total += marks[i];
        }
   
        // Calculate the percentage.
        // Since all the marks are integer,
        // typecast the calculation to double.
        let percentage
            = ((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';
                    }
                }
            }
        }
   
        document.write(grade);
 
 
// This code is contributed by rag2127
</script>
Producción: 

C

 

Publicación traducida automáticamente

Artículo escrito por AbhijeetSridhar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *