Java Program to Find Division of students based on Marks

In java, to find division of the students based on marks we can use if-else if conditional statements or Ternary Operator. In this article, we will learn through examples, algorithm, logic and program explanation for better understanding.

For example, if a student secures 65 marks in the exam, then the student achieves the first division.

Required Knowledge

  • Java programming basics
  • Java Operators
  • Conditional Statements(if-else and Switch)
  • Ternary Operator

Problem Statement

We need to write a Java program to check the division of students based on their marks. The program should take any value as marks and output the corresponding division. The above action should be performed by our program. Let’s look an example,

Example 1:

Input: 37
Student Secured Division 3

Here for the input 37, Output is “Student Secured Division 3” as the range of marks 30 to 39 falls under Division 3 category.

Example 2:

Input: 28
Student failed

Here for the input 28, Output is “Student failed” as the scores below or equal to 29 falls under failed category.

Algorithm to check division of students based on Marks

  • Start: Take an input value as marks
  • Condition Check:
    • marks>=60 && marks<=100, it prints “Student achieved Division 1”
    • marks>=40 && marks<=59, it prints “Student achieved Division 2”
    • marks>=30 && marks<=39, it prints “Student achieved Division 3”
    • marks<=29, it prints “Student failed”
  • End: Now java terminates the program.

Logic to check division of students based on Marks

There are multiple logics to check division of students based on marks in java programming language.

Logic 1: Using if-else if

  • After taking marks as input, we will check, if the marks are in between 60 to 100, then we will say, student secured division 1. Otherwise, we will check next condition that whether the marks falls within 40 to 59, we will say student secured division 2.
  • If not, we will check another condition that marks falls within the range 30 to 39 and we say student secured division 3. Otherwise, if marks are less than or equal to 29, then we say student failed. Else we ask the user to enter valid marks.
if(marks>=60 && marks<=100)
			System.out.println(marks+" Student secured Divison 1");
		else if(marks>=40 && marks<=59)
			System.out.println(marks+" Student secured Division 2");
		else if(marks>=30 && marks<=39)
			System.out.println(marks+" Student Secured Division 3");
		else if(marks<=29)
			System.out.println(marks+" Student failed");
		else 
			System.out.println(marks+ " Invalid marks");

Logic 2: Using Ternary Operator

  • This operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
  • Take an input value as marks from the user. Use a ternary operator to determine the result based on the input marks. Assign output to the result variable. If the input marks are not within the specified ranges, it will print “Invalid marks”.
String result;
		result = (marks >= 60 && marks<=100) ? marks + " Student secured Divison 1"
				: (marks >= 40 && marks<=49) ? marks + " Student secured Division 2"
						: (marks >= 30 && marks<=39) ? marks + " Student Secured Division 3" : 
							(marks<=29) ? marks +" Student failed":marks+" Invalid marks";
		System.out.println(result);

Java program to check division of students based on Marks

Program 1: Using if- else if

In the below program we are using if else if to check division of students based on marks in Java.

import java.util.Scanner;

public class DivisionBasedOnMarks {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Please enter the percentage of marks in numbers");
		int marks = s.nextInt();
		if(marks>=60 && marks<=100)
			System.out.println(marks+" Student secured Divison 1");
		else if(marks>=40 && marks<=59)
			System.out.println(marks+" Student secured Division 2");
		else if(marks>=30 && marks<=39)
			System.out.println(marks+" Student Secured Division 3");
		else if(marks<=29)
			System.out.println(marks+" Student failed");
		else 
			System.out.println(marks+ " Invalid marks");
		s.close();
	}

}

Output:

Please enter the percentage of marks in numbers
58
58 Student secured Division 2

Java program explanation to check Division based on Student Marks

  • This Java program takes input from the user in the form of percentage marks.
  • It asks the user to enter the percentage marks.
  • It then reads the input using a Scanner object.
  • Based on the input percentage, the program evaluates which division the student falls into.
  • If the percentage is between 60 and 100 (inclusive), it prints that the student secured Division 1.
  • Else if the percentage is between 40 and 59 (inclusive), it prints that the student secured Division 2.
  • Otherwise, if the percentage is between 30 and 39 (inclusive), it prints that the student secured Division 3.
  • If the percentage is less than or equal to 29, it prints that the student failed.
  • Otherwise, if the input is not within the valid percentage range (0 to 100), it prints “Invalid marks.”
  • Finally, the program closes the Scanner object to release system resources.

Program 2: Using Ternary Operator

In the below program we are using Ternary Operator to check division of students based on marks in Java.

import java.util.Scanner;

public class DivisionBasedOnMarks {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Please enter the percentage of marks in numbers");
		int marks = s.nextInt();
		String result;
		result = (marks >= 60 && marks<=100) ? marks + " Student secured Divison 1"
				: (marks >= 40 && marks<=49) ? marks + " Secured Division 2"
						: (marks >= 30 && marks<=39) ? marks + " Student Secured Division 3" : 
							(marks<=29) ? marks +" Student failed":marks+" Invalid marks";
		System.out.println(result);
		s.close();
	}

}

Output:

Please enter the percentage of marks in numbers
13
13 Student failed

Java program explanation to check Division based on Student Marks

  • This Java program is similar to the previous one but uses the ternary conditional operator to determine the division based on the input marks.
  • It asks the user to enter the percentage marks.
  • It then reads the input using a Scanner object.
  • A String variable result is initialized to store the division result.
  • The ternary conditional operator ? : is used to evaluate the condition and assign the appropriate division result to the result variable.
  • If the percentage is between 60 and 100 (inclusive), it assigns the result that the student secured Division 1.
  • Else, if the percentage is between 40 and 49 (inclusive), it assigns the result that the student secured Division 2.
  • If the percentage is between 30 and 39 (inclusive), it assigns the result that the student secured Division 3.
  • Otherwise, if the percentage is less than or equal to 29, it assigns the result that the student failed.
  • If the input is not within the valid percentage range (0 to 100), it assigns the result “Invalid marks.”
  • Finally, the program prints the result and closes the Scanner object to release system resources.

Conclusion:

In this article, we explored how to find division of students based on their marks using if-else if statements and the ternary operator. We understood the logic and moved on to writing the program.

For more information on related topics, you might find these articles helpful:

We hope you find this article helpful. If you have any doubts or need any further clarifications, feel free to leave a comment. Don’t forget to subscribe our newsletter to get more updates on Java Programming tips and tutorials.

Happy Java Programming!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.