Table of Contents
ToggleIn this article, we will see how to calculate Student Percentage And Division In Java. We will see various percentage division program in java.
Let’s dive in…
Problem Statement
Write a Java program to take marks scored out of 100 by a student in 5 subjects as input, calculate the overall percentage, and print the division obtained by the student based on the following criteria:
- If percentage is greater than or equal to 60, print “First Division“.
- If percentage is between 50 and 59, print “Second Division“.
- If percentage is between 40 and 49, print “Third Division“.
- If percentage is less than 40, print “Fail”.
For Example
Input:
Enter marks in 5 subjects:
75
82
56
48
92
Output:
Percentage: 70.6
Division: First Division
Explanation
- Input is taken as marks in 5 subjects.
- Total marks = 75 + 82 + 56 + 48 + 92 = 353
- Percentage = (Total Marks/500) x 100 = (353/500) x 100 = 70.6
- Since percentage is 70.6 which is greater than 60, “First Division” is printed.
Solution
We will explore different approaches in Java to calculate 1st 2nd 3rd division percentage. These are:
- Using if-else Statements
- Using Ternary operator
Approach 1: Using if-else
In this approach we will use if else to handle conditions of each division. Each condition is checked one by one using if-else statements.
Java Code Implementation
// Percentage Division Program in Java using if-else
import java.util.Scanner;
public class StudentMarks {
public static void main(String[] args) {
// Initialize a Scanner object to get user input
Scanner scanner = new Scanner(System.in);
double totalMarks = 0;
// Loop through 5 times to take input for each subject
for (int i = 1; i <= 5; i++) {
System.out.print("Enter marks for subject " + i + ": ");
totalMarks += scanner.nextDouble(); // Add each subject's marks to the total
}
// Calculate the percentage
double percentage = totalMarks / 5;
// Initialize a variable to store the division
String division;
// Check percentage range and assign the appropriate division
if (percentage >= 60) {
division = "First Division";
} else if (percentage >= 50) {
division = "Second Division";
} else if (percentage >= 40) {
division = "Third Division";
} else {
division = "Fail";
}
// Print the calculated percentage and determined division
System.out.printf("Percentage: %.1f\nDivision: %s\n", percentage, division);
}
}
Output
Percentage: 70.6
Division: First Division
Code Explanation
- The program uses the Scanner class from the java.util package, which provides methods to read input values from the user.
- To determine the division of a student, we need to check the percentage scored against a set of conditions. If-else statements allow us to evaluate each condition explicitly in a readable way.
- The first if statement checks if the percentage is greater than or equal to 60. If this condition is true, the student falls in the “First Division” category else next if conditions are checked.
- Finally, if none of if-else conditions are met, it means the percentage is less than 40 and the student has failed. The else block handles this scenario by storing “Fail” in the division variable.
- The output are stored in percentage and division variable.
Approach 2: Using Ternary operator
The above solution can be optimised to reduce number of lines of code by using ternary operators.
Java Code Implementation
// Percentage Division Program in Java using ternary operator
import java.util.Scanner;
public class StudentMarks {
public static void main(String[] args) {
// Initialize a Scanner object to get user input
Scanner scanner = new Scanner(System.in);
double totalMarks = 0;
// Loop through 5 times to take input for each subject
for (int i = 1; i <= 5; i++) {
System.out.print("Enter marks for subject " + i + ": ");
totalMarks += scanner.nextDouble(); // Add each subject's marks to the total
}
// Calculate the percentage
double percentage = totalMarks / 5;
// Use the ternary operator to determine division based on the percentage
String division = percentage >= 60 ? "First Division"
: percentage >= 50 ? "Second Division"
: percentage >= 40 ? "Third Division"
: "Fail";
// Print the calculated percentage and determined division
System.out.printf("Percentage: %.1f\nDivision: %s\n", percentage, division);
}
}
Output
Percentage: 70.6
Division: First Division
Code Explanation
- A nested ternary operator works in sequence. It checks the first condition (
percentage >= 60
). If that’s true, it returns “First Division“. If not, it moves to the next condition, and so on until it finds a true condition or reaches the end. If it reaches the end, “Fail” is returned. - This is a concise way to check multiple conditions in order and return a value based on the first condition that’s true.
Key Takeaways
- To calculate percentage, total marks obtained is divided by total maximum marks and multiplied by 100.
- if-else statements can be used to check different percentage ranges and determine the division. Each condition is checked explicitly.
- Ternary operator can optimize the code by checking multiple conditions concisely in order. It returns the first value where condition is true.
Similar Posts
Add Days To The Current Date in Java
Calculate Area of Hexagon in Java
Program to Calculate EMI in Java
Calculate Area of a Circle in Java
Checkout more Java Tutorials here.
I hope You liked the post ?. For more such posts, ? subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.
FAQ
How to take multiple inputs from user in Java?
Use a for loop and Scanner class to take multiple inputs. Initialize a Scanner object. Use a for loop with counter variable and loop through it to take input using scanner.nextDouble() inside the loop.
How are the percentage ranges calculated for divisions?
The percentage ranges for divisions are:
First Division: >= 60%
Second Division: 50 – 59%
Third Division: 40 – 49%
Fail: < 40%
How to print formatted output in Java?
Use System.out.printf() to print formatted output in Java. It allows formatting strings using format specifiers like %d for integer, %f for float. We can also specify number of decimal places like %.2f.