GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||
GeezerVison is showing codescracker.com — Java Program to Calculate Grades of Students
Java Program to Calculate Grades of StudentsThis post covers a program in Java that finds and prints the grade of a student based on the marks entered by the user at run-time of the program. The grades of students must be calculated as per the following table:
Note: In the above table, the range >= 90 and < 94 means that if the average mark is greater than or equal to 90 and less than 94, then the grade should be A-. Under this range, some marks may lie between 90, 90.23, 92, 93.89, etc. That is, in the range of 90-94 here, 90 is included, whereas 94 is excluded. Find Grades of Students in Java: Basic VersionThe question is: write a program in Java to find the grade of a student using if-else. The program given below has its answer: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { float[] marks = new float[8]; float sum=0, avg; int i; Scanner scan = new Scanner(System.in); System.out.print("Enter Marks Obtained in 8 Subjects: "); for(i=0; i<8; i++) marks[i] = scan.nextFloat(); for(i=0; i<8; i++) sum = sum + marks[i]; avg = sum/8; System.out.print("\nGrade = "); if(avg>=94) System.out.println("A"); else if(avg>=90 && avg<94) System.out.println("A-"); else if(avg>=87 && avg<90) System.out.println("B+"); else if(avg>=83 && avg<87) System.out.println("B"); else if(avg>=80 && avg<83) System.out.println("B-"); else if(avg>=77 && avg<80) System.out.println("C+"); else if(avg>=73 && avg<77) System.out.println("C"); else if(avg>=70 && avg<73) System.out.println("C-"); else if(avg>=67 && avg<70) System.out.println("D+"); else if(avg>=63 && avg<67) System.out.println("D"); else if(avg>=60 && avg<63) System.out.println("D-"); else System.out.println("F"); } } The snapshot given below shows the initial output produced by the above Java program: Now supply the marks obtained in 8 subjects. Here is its sample run, after providing marks like 94, 97, 92, 89, 86, 98, 99, and 88 as eight subject marks: The above program can also be created in this way: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { float[] marks = new float[8]; float sum=0; Scanner scan = new Scanner(System.in); System.out.print("Enter Marks Obtained in 8 Subjects: "); for(int i=0; i<8; i++) { marks[i] = scan.nextFloat(); sum += marks[i]; } float avg = sum/8; String grade = ""; if(avg>=94) grade = "A"; else if(avg>=90 && avg<94) grade = "A-"; else if(avg>=87 && avg<90) grade = "B+"; else if(avg>=83 && avg<87) grade = "B"; else if(avg>=80 && avg<83) grade = "B-"; else if(avg>=77 && avg<80) grade = "C+"; else if(avg>=73 && avg<77) grade = "C"; else if(avg>=70 && avg<73) grade = "C-"; else if(avg>=67 && avg<70) grade = "D+"; else if(avg>=63 && avg<67) grade = "D"; else if(avg>=60 && avg<63) grade = "D-"; else grade = "F"; System.out.println("\nGrade = " +grade); } } Find Grades of Students in Java: Complete VersionSince the program given above has some limitations, such as what if the user enters a mark greater than 100? import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { float[] marks = new float[8]; float sum=0; Scanner scan = new Scanner(System.in); System.out.print("Enter Total Number of Subjects: "); int tot = scan.nextInt(); System.out.print("Enter Marks Obtained in " +tot+ " Subjects: "); for(int i=0; i<tot; i++) { marks[i] = scan.nextFloat(); sum += marks[i]; } float avg = sum/tot; String grade = ""; if(avg>=94 && avg<=100) grade = "A"; else if(avg>=90 && avg<94) grade = "A-"; else if(avg>=87 && avg<90) grade = "B+"; else if(avg>=83 && avg<87) grade = "B"; else if(avg>=80 && avg<83) grade = "B-"; else if(avg>=77 && avg<80) grade = "C+"; else if(avg>=73 && avg<77) grade = "C"; else if(avg>=70 && avg<73) grade = "C-"; else if(avg>=67 && avg<70) grade = "D+"; else if(avg>=63 && avg<67) grade = "D"; else if(avg>=60 && avg<63) grade = "D-"; else if(avg>=0 && avg<60) grade = "F"; else grade = "Invalid Input!"; if(grade.equals("Invalid Input!")) System.out.println("\nInvalid Input!"); else System.out.println("\nGrade = " +grade); } } Here is its sample run with user input 5 as the number of subjects and 70, 78, 90, 94, and 88 as the five subject marks: Same Program in Other Languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. | ||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |