Home > Java > javaTutorial > Java program example to calculate total score and percentage

Java program example to calculate total score and percentage

WBOY
Release: 2023-09-11 18:01:02
forward
1146 people have browsed it

Java program example to calculate total score and percentage

We will demonstrate how to calculate the total score and percentage using a Java program. Total Score refers to the sum of all available points, while the term Percent refers to the calculated score divided by the total score and multiplied by the resulting number of 100.

percentage_of_marks = (obtained_marks/total_marks) × 100
Copy after login

Example 1

This is a Java program that demonstrates how to calculate total scores and percentages.

// Java Program to demonstrate how is Total marks and Percentages calculated
import java.io.*;
public class TotalMarks_Percent1 {
	public static void main(String[] args){
		int n = 8, t_marks = 0;
		float percent;
		// creation of 1-D array to store marks
		int marks[] = { 69, 88, 77, 89, 98, 100, 57, 78 };
		// calculation of total marks
		for (int j = 0; j < n; j++) {
			t_marks += marks[j];
		}
		System.out.println("Total Marks is: " + t_marks);
		// calculate the percentage
		percent = (t_marks / (float)n);
		System.out.println("Total Percentage is: " + percent + "%");
	}
}
Copy after login

Output

Total Marks is: 656
Total Percentage is: 82.0%
Copy after login

In the above Java program, the total marks and percentage obtained by the students in 8 subjects are being calculated. The obtained scores are stored in an array named marks[].

Total scores are calculated and stored in a variable called t_marks, and their percentages are calculated and stored in a variable called percent.

Both values ​​will be further displayed on the console.

The Chinese translation of

Example 2

is:

Example 2

This is a Java program that demonstrates calculating the total score and percentage of five subjects from user input.

// Java program to compute the total marks and percentage of five subjects taken as input from the user
import java.util.Scanner;
class TotalMarks_Percent2{
public static void main(String args[]){
    float FLAT, COA, Networking, Python, AI; 
    double t_marks, percent;
    Scanner mk =new Scanner(System.in);
    // Take marks as input of 5 subjects from the user 
    System.out.println("Input the marks of five subjects \n");
    System.out.print("Enter marks of FLAT:");
    FLAT = mk.nextFloat();
    System.out.print("Enter marks of COA:");
    COA = mk.nextFloat();
    System.out.print("Enter marks of Networking:");
    Networking = mk.nextFloat();
    System.out.print("Enter marks of Python:");
    Python = mk.nextFloat();
    System.out.print("Enter marks of AI:");
    AI = mk.nextFloat();
    // Calculation of total marks and percentage obtained in 5 subjects
    t_marks = FLAT + COA + Networking + Python + AI;
    percent = (t_marks / 500.0) * 100;
    // display the results 
    System.out.println("Total marks obtained in 5 different subjects ="+t_marks);    
    System.out.println("Percentage obtained in these 5 subjects = "+percent);
   }
}
Copy after login

Output

Input the marks of five subjects 

Enter marks of FLAT:98
Enter marks of COA:56
Enter marks of Networking:67
Enter marks of Python:89
Enter marks of AI:78
Total marks obtained in 5 different subjects =388.0
Percentage obtained in these 5 subjects = 77.60000000000001
Copy after login

In the above Java program, the results of 5 different subjects are input from the user, namely FLAT, COA, Networking, Python and AI.

Marks the user's input and stores it in a variable of floating point data type. Furthermore, the total marks obtained in these subjects are calculated by adding the marks of each subject and stored in a variable named t_marks.

Finally, the program will display the total score of the specified subject and its percentage.

This article clarifies two methods of calculating the total score and its percentage. The article begins by discussing semester percentages and total grades. The first method discusses a method that does not accept user input, while the second method takes the value of the score as user input, calculates and displays the sum and percentage of the score.

The above is the detailed content of Java program example to calculate total score and percentage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template