Friday, May 10, 2024
HomeProgrammingJava program to calculate students grades in 2 different ways

Java program to calculate students grades in 2 different ways

Java program to calculate students grades:

In this post we will learn different ways to calculate the figure from a student in Java. We will write one Java program that will ask the user to enter the grades of different subjects for a student. Then it calculates the average of the grades and prints the grade based on the average value.

We will use predefined figure charting, that is, the numbers are defined before the average is calculated. The number of subjects may be different for different students. This program will therefore also use the number of boxes as input.

Defining the numbers:

The program uses the following average grades and grade assignment:

Average gradesFigure
>=80a
>=60 and <80b
>=30 and <60c
<30D

To find the average grades, we need to add the grades together and divide the total amount by the number of subjects.

Example 1: Java program to calculate students grade using a for loop:

In this program we use a for run to calculate the grade. Below you will find the full program:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        float totalSubjects, totalMarks = 0, averageMarks;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the total number of subjects: ");
        totalSubjects = scanner.nextFloat();

        for (int i = 1; i <= totalSubjects; i++) {
            System.out.println("Enter the marks for subject " + i + ": ");
            totalMarks += scanner.nextFloat();
        }

        averageMarks = totalMarks / totalSubjects;

        if (averageMarks >= 80) {
            System.out.println("Average Marks: " + averageMarks + ", Grade A");
        } else if (averageMarks >= 60) {
            System.out.println("Average Marks: " + averageMarks + ", Grade B");
        } else if (averageMarks >= 30) {
            System.out.println("Average Marks: " + averageMarks + ", Grade C");
        } else {
            System.out.println("Average Marks: " + averageMarks + ", Grade D");
        }
    }
}

Here,

  • We created three floating point variables, totalTopics is used to track the total number of topics, total figures is used to keep track of the total grades for all subjects and average brands is used to keep track of the average grades of all subjects. Also a Scanner An object is created to read the user’s input values.
  • Once the user enters the total number of topics, he reads that value and assigns it to the totalTopics variable.
  • The for run is used to read the numbers of the boxes one by one. It asks the user to enter the markers and adds that value to the total figures variable.
  • The average grade is calculated by dividing the total number of points by the total number of subjects.
  • The last as, else-as, else statements are used to find the number. It compares the average grade and prints the grade and the calculated average grades.

This program produces the following output:

Enter the total number of subjects: 
3
Enter the marks for subject 1: 
92
Enter the marks for subject 2: 
85
Enter the marks for subject 3: 
89
Average Marks: 88.666664, Grade A

Enter the total number of subjects: 
3
Enter the marks for subject 1: 
87
Enter the marks for subject 2: 
88
Enter the marks for subject 3: 
35
Average Marks: 70.0, Grade B

This program works with any number of topics.

Example 2: Java program to calculate a student’s grade using separate functions:

We can also use a separate function to calculate the student’s grade with separate functions. We can create one function to read the numbers and another function to print the numbers. Let’s write down the program:

import java.util.Scanner;

public class Main {

    private static float getAverage() {
        float totalSubjects, totalMarks = 0, averageMarks;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the total number of subjects: ");
        totalSubjects = scanner.nextFloat();

        for (int i = 1; i <= totalSubjects; i++) {
            System.out.println("Enter the marks for subject " + i + ": ");
            totalMarks += scanner.nextFloat();
        }

        return totalMarks / totalSubjects;
    }

    private static void printGrade(float averageMarks) {
        if (averageMarks >= 80) {
            System.out.println("Average Marks: " + averageMarks + ", Grade A");
        } else if (averageMarks >= 60) {
            System.out.println("Average Marks: " + averageMarks + ", Grade B");
        } else if (averageMarks >= 30) {
            System.out.println("Average Marks: " + averageMarks + ", Grade C");
        } else {
            System.out.println("Average Marks: " + averageMarks + ", Grade D");
        }
    }

    public static void main(String[] args) {
        printGrade(getAverage());
    }
}

We created two functions:

  • The getAverage function reads the grades and calculates the average grade. It returns the average grade.
  • The print quality The function takes the average grade as a parameter and prints the grade.
  • In the mainly function we call the print quality function and the value it returns getAverage is passed on.

With this program you will get a similar result.

RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

бнанс Створити акаунт on The Interview Process in Clark, Pampanga
Binance注册奖金 on Venus in Scorpio