TeachingBee

Student Percentage And Division Program In Java

Percentage And Division Program In Java-teachingbee

In 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:

  1. Using if-else Statements
  2. 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

  1. The program uses the Scanner class from the java.util package, which provides methods to read input values from the user.
  2. 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.
  3. 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.
  4. 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.
  5. 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

  1. 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.
  2. 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

Reverse A Number 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?

How are the percentage ranges calculated for divisions?

How to print formatted output in Java?

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

ascii value in java

Print ASCII Value in Java

In this article we will into the ASCII table in C, exploring its various character sets—from control characters to printable characters, including letters, digits, and special symbols—and demonstrates how to

data hiding in java

Data Hiding In Java

In this article we will discuss data hiding in Java which is an important concept in object-oriented programming. We will cover So let’s get started. What is Data Hiding in

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In