Program to check if a student passes/fails using his grade | Menu Driven
Last Updated :
10 Mar, 2024
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();
cin >> input;
}
cout << "Program Terminated" << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class GradeChecker {
public static void main(String[] args) {
char input = 'Y' ;
Scanner scanner = new Scanner(System.in);
while ( true ) {
System.out.print( "Enter student's grade: " );
if (!scanner.hasNext()) {
break ;
}
if (scanner.hasNextInt()) {
int grades = scanner.nextInt();
if (grades > 100 || grades < 0 ) {
System.out.println( "Invalid grades" );
} else if (grades >= 33 ) {
System.out.println( "Pass" );
} else {
System.out.println( "Fail" );
}
} else {
String invalidInput = scanner.next();
System.out.println( "Invalid input: " + invalidInput);
continue ;
}
System.out.println( "To continue, press Y" );
System.out.println( "Press any other key to exit" );
if (scanner.hasNext()) {
input = scanner.next().charAt( 0 );
if (input != 'Y' ) {
break ;
}
} else {
break ;
}
}
System.out.println( "Program Terminated" );
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
const readline = require( 'readline' );
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = 'Y' ;
function checkGrades(grades) {
if (grades > 100 || grades < 0) {
console.log( "Invalid grades" );
} else if (grades >= 33) {
console.log( "Pass" );
} else {
console.log( "Fail" );
}
}
function promptUser() {
rl.question( "Enter student's grade: " , (grade) => {
if (!isNaN(parseInt(grade))) {
const grades = parseInt(grade);
checkGrades(grades);
} else {
console.log( "Invalid input: " + grade);
}
console.log( "To continue, press Y" );
console.log( "Press any other key to exit" );
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 :
grades = int ( input ( "Enter student's grade: " ))
except EOFError:
print ( "No input provided. Exiting..." )
break
except ValueError:
print ( "Invalid input. Please enter a valid integer." )
continue
if grades > 100 or grades < 0 :
print ( "Invalid grades" )
elif grades > = 33 :
print ( "Pass" )
else :
print ( "Fail" )
input_value = input ( "To continue press 'Y'. Press any other key to exit: " )
if input_value.upper() ! = 'Y' :
break
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
Share your thoughts in the comments
Please Login to comment...