- Java Basic Programs
- Java Programming Examples
- Java Print Hello World
- Java Get Input from User
- Java Print Integer
- Java Add two Numbers
- Java Check Even or Odd
- Java Check Prime or Not
- Java Check Alphabet or Not
- Java Check Vowel or Not
- Check Reverse equal Original
- Java Fahrenheit to Celsius
- Java Celsius to Fahrenheit
- Java Perfect Number Program
- Java Find Quotient Remainder
- Java Days to Seconds
- Java Count Digits in Number
- Java Binary Number Addition
- Java Discount Program
- Java Compute Courier Charge
- Java Find Telephone Bill
- Java Print ASCII Values
- Java Check Palindrome or Not
- Java Check Armstrong or Not
- Generate Armstrong Numbers
- Add two Numbers using Pointers
- Java Mathematical Programs
- Add Subtract Multiply & Divide
- Java Make Calculator
- Java Add Digits of Number
- Java Check Leap Year or Not
- Java Check Divisibility
- Java Find Simple Interest
- Java Find Compound Interest
- Java Print Fibonacci Series
- Java Find nCr nPr
- Calculate Average & Percentage
- Java Calculate Arithmetic Mean
- Java Calculate Student Grade
- Java Print Table of Number
- Java Print Prime Numbers
- Java Add n Numbers
- Java Interchange two Numbers
- Java Reverse Numbers
- Java Swap two Numbers
- Count Positive Negative & Zero
- Find Largest of two Numbers
- Find Largest of three Numbers
- Java Find Factorial of Number
- Java Find HCF & LCM
- Area & Perimeter of Square
- Area & Perimeter of Rectangle
- Area & Circumference of Circle
- Java Conversion Programs
- Java Decimal to Binary
- Java Decimal to Octal
- Java Decimal to Hexadecimal
- Java Binary to Decimal
- Java Binary to Octal
- Java Binary to Hexadecimal
- Java Octal to Decimal
- Java Octal to Binary
- Java Octal to Hexadecimal
- Java Hexadecimal to Decimal
- Java Hexadecimal to Binary
- Java Hexadecimal to Octal
- Java Pattern Programs
- Java Pattern of Stars
- Java Pattern of Alphabets
- Java Pattern of Numbers
- Java Pyramid of Stars
- Java Pyramid of Alphabets
- Java Pyramid of Numbers
- Java Print Diamond Pattern
- Java Print Floyd Triangle
- Java Print Pascal Triangle
- Java Array Programs
- One Dimensional Array Program
- Java Linear Search
- Java Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Java Reverse Array
- Insert Element in Array
- Delete Element from Array
- Java Merge two Array
- Java Bubble Sort
- Java Selection Sort
- Java Insertion Sort
- Java Find Common Elements
- Java Count Even/Odd Number
- Two Dimensional Array Program
- Java Add two Matrices
- Java Subtract two Matrices
- Java Transpose Matrix
- Multiply two Matrices
- Three Dimension Array Program
- Java String Programs
- Java Print String
- Find Length of String
- Java Compare two String
- Java Copy String
- Java Concatenate String
- Java Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Occurrence of a Character
- Java Find Occurrence of a Word
- Occurrence of Each Character
- Java Occurrence of Each Word
- Java Count Repeated Characters
- Java Count Repeated Words
- Java Capitalize Each Word
- Java Count Vowels/Consonants
- Java Extract Numbers
- Java Count Word in String
- Remove Spaces from String
- Java Sort a String
- Java Uppercase to Lowercase
- Java Lowercase to Uppercase
- Java Swap two Strings
- Java Check Anagram or Not
- Java Check Balance Parentheses
- Java Check Password Strength
- Java File Programs
- Java Read File
- Java Write to File
- Read & Display File Content
- Java Copy File
- Java Append Text to File
- Java Merge two File
- List files in Directory
- Java Delete File
- Java Miscellaneous Programs
- Generate Random Numbers
- Java Print Time & Date
- Java Get IP Address
- Java Shutdown Computer
- Java Programming Tutorial
- Java Tutorial
Java Program to Calculate Grades of Student
This post covers a program in Java that find and prints the grade of a student based on the marks entered by user at run-time of the program.
The grades of student must be calculated as per the following table:
Average Mark Range | Grade |
---|---|
>= 94 | A |
>= 90 and < 94 | A- |
>= 87 and < 90 | B+ |
>= 83 and < 87 | B |
>= 80 and < 83 | B- |
>= 77 and < 80 | C+ |
>= 73 and < 77 | C |
>= 70 and < 73 | C- |
>= 67 and < 70 | D+ |
>= 63 and < 67 | D |
>= 60 and < 63 | D- |
< 60 | F |
Note - In above table, the range >= 90 and < 94 means, 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 are 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 Student in Java - Basic Version
The question is, write a program in Java to find grade of student using if-else. The program given below is its answer:
import java.util.Scanner; public class fresherearth { 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 above Java program:
Now supply the marks obtained in 8 subjects. Here is its sample run, after providing marks say 94, 97, 92, 89, 86, 98, 99, 88 as eight subject marks:
The above program can also be created in this way:
import java.util.Scanner; public class fresherearth { 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 Student in Java - Complete Version
Since the program given above has some limitations such as what if user enters a mark, greater than 100 ?
also the program works on 8 subjects only ?
Therefore keeping in mind, these types of limitations, I've modified the program and created a new one as given below.
import java.util.Scanner; public class fresherearth { 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 number of subjects and 70, 78, 90, 94, 88 as five subject marks:
Same Program in Other Languages
« Previous Program Next Program »