Open In App

Program to check if a student passes/fails using his grade | Menu Driven

Last Updated : 10 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Write a Menu-driven program to check Whether a student passes/fails using his/her grade.
Condition to pass: Grade should be greater than or equal to 33.
Valid grade range: 0 to 100

A menu-driven program is a type of computer program that allows users to interact with it by selecting options from a menu. Instead of typing commands or code, users navigate through a series of menus that present a list of actions or functions they can perform.

Examples:

Enter students grade: 150
Invalid grades
To continue press Y
Press any other key to exit
Y
Enter students grade: -509
Invalid grades
To continue press Y
Press any other key to exit
Y
Enter students grade: 45
Pass
To continue press Y
Press any other key to exit
Y
Enter students grade: 12
Fail
To continue press Y
Press any other key to exit
N
Program Terminated

Approach: To solve the problem, follow the below idea:

The problem can be solved using a while loop which keeps on asking the user to input the grade. Inside the while loop, the user enters the grade and if the grade < 33, the user fails else the user passes. After every result, the user is prompted if he wants to continue entering the grades or exit the program. The program exits if the user inputs anything except “Y”.

Below is the implementation of the approach:

C++




#include <iostream>
using namespace std;
 
int main()
{
    char input = 'Y';
    int grades;
    while (input == 'Y')
    {
        cout << "Enter student's grade: ";
        cin >> grades;
 
        if (grades > 100 || grades < 0)
        {
            cout << "Invalid grades" << endl;
        }
        else if (grades >= 33)
        {
            cout << "Pass" << endl;
        }
        else
        {
            cout << "Fail" << endl;
        }
        cout << "To continue press Y" << endl;
        cout << "Press any other key to exit" << endl;
        cin.ignore(); // Ignore any remaining characters in the input buffer
        cin >> input;
    }
    cout << "Program Terminated" << endl;
 
    return 0;
}


Java




import java.util.Scanner;
 
public class GradeChecker {
    public static void main(String[] args) {
        // Initialize variables
        char input = 'Y';
        Scanner scanner = new Scanner(System.in);
 
        // Loop to allow checking multiple grades
        while (true) {
            // Prompt user to enter student's grade
            System.out.print("Enter student's grade: ");
 
            // Check if the scanner has next token
            if (!scanner.hasNext()) {
                // If no input is available, break out of the loop
                break;
            }
 
            // Check if the next token is an integer
            if (scanner.hasNextInt()) {
                int grades = scanner.nextInt();
 
                // Check if the entered grade is valid
                if (grades > 100 || grades < 0) {
                    System.out.println("Invalid grades");
                } else if (grades >= 33) {
                    System.out.println("Pass");
                } else {
                    System.out.println("Fail");
                }
            } else {
                // If input is not an integer, consume and ignore it
                String invalidInput = scanner.next();
                System.out.println("Invalid input: " + invalidInput);
                continue;
            }
 
            // Ask the user if they want to continue
            System.out.println("To continue, press Y");
            System.out.println("Press any other key to exit");
 
            // Read the user's choice for continuation
            if (scanner.hasNext()) {
                input = scanner.next().charAt(0);
                if (input != 'Y') {
                    break;
                }
            } else {
                // If no input is available, break out of the loop
                break;
            }
        }
 
        // Display a message when the program is terminated
        System.out.println("Program Terminated");
 
        // Close the scanner to prevent resource leaks
        scanner.close();
    }
}


C#




using System;
 
class Program
{
    static void Main(string[] args)
    {
        char input = 'Y';
        int grades;
 
        while (input == 'Y')
        {
            Console.Write("Enter student's grade: ");
            grades = Convert.ToInt32(Console.ReadLine());
 
            if (grades > 100 || grades < 0)
            {
                Console.WriteLine("Invalid grades");
            }
            else if (grades >= 33)
            {
                Console.WriteLine("Pass");
            }
            else
            {
                Console.WriteLine("Fail");
            }
 
            Console.WriteLine("To continue, press Y");
            Console.WriteLine("Press any other key to exit");
            input = Console.ReadKey().KeyChar;
            Console.WriteLine();
        }
 
        Console.WriteLine("Program Terminated");
    }
}


Javascript




// Import the readline module for reading input from the console
const readline = require('readline');
 
// Create an interface to read input from the console
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
 
// Initialize variables
let input = 'Y';
 
// Function to check grades
function checkGrades(grades) {
    // Check if the entered grade is valid
    if (grades > 100 || grades < 0) {
        console.log("Invalid grades");
    } else if (grades >= 33) {
        console.log("Pass");
    } else {
        console.log("Fail");
    }
}
 
// Function to prompt user for input
function promptUser() {
    // Prompt user to enter student's grade
    rl.question("Enter student's grade: ", (grade) => {
        // Check if the input is an integer
        if (!isNaN(parseInt(grade))) {
            // Convert input to integer
            const grades = parseInt(grade);
            // Check the grade
            checkGrades(grades);
        } else {
            // If input is not an integer
            console.log("Invalid input: " + grade);
        }
 
        // Ask the user if they want to continue
        console.log("To continue, press Y");
        console.log("Press any other key to exit");
 
        // Read the user's choice for continuation
        rl.question("", (response) => {
            input = response.toUpperCase();
            if (input !== 'Y') {
                // Close the interface
                rl.close();
            } else {
                // Prompt user again
                promptUser();
            }
        });
    });
}
 
// Start the program
promptUser();
 
// Display a message when the program is terminated
rl.on('close', () => {
    console.log("Program Terminated");
});


Python3




while True:
    try:
        # Get input from the user and convert it to an integer
        grades = int(input("Enter student's grade: "))
    except EOFError:
        # Handle EOF (End of File) condition
        print("No input provided. Exiting...")
        break
    except ValueError:
        # Handle the case where the input is not a valid integer
        print("Invalid input. Please enter a valid integer.")
        continue
 
    # Check if the grades are outside the valid range
    if grades > 100 or grades < 0:
        print("Invalid grades")
    # Check if the grades are equal to or greater than the passing threshold
    elif grades >= 33:
        print("Pass")
    else:
        # If grades are below the passing threshold
        print("Fail")
 
    # Prompt the user to continue or exit the program
    input_value = input("To continue press 'Y'. Press any other key to exit: ")
 
    # Check if the user wants to exit the program
    if input_value.upper() != 'Y':
        break
 
# Display a message when the program is terminated
print("Program Terminated")


Output:

Enter students grade: 150
Invalid grades
To continue press Y
Press any other key to exit
Y
Enter students grade: -509
Invalid grades
To continue press Y
Press any other key to exit
Y
Enter students grade: 45
Pass
To continue press Y
Press any other key to exit
Y
Enter students grade: 12
Fail
To continue press Y
Press any other key to exit
N
Program Terminated


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads