Java Program to Calculate Percentage

In this program, we will learn to code the Java Program to Calculate Percentage. Let’s understand How to calculate the percentage in Java Programming Language. Let’s understand more about Percentage and How Percentage is Calculated?

Percentage

A Percentage is denoted by % symbol. In Mathematics, a percentage is a number or ratio expressed as a fraction of 100.

How to Calculate Percentage?

The formula to calculate percentage is as follows

Percentage = (Value / Total Value) X 100

For example, let’s consider a student who has obtained 92 marks out of 120. So, the percentage scored by the student will be calculated as,

Percentage scored by student = (92 / 120) X 100 = 76.67 %

Now, let’s get straight into the Java Program to Calculate Percentage.

Java Program to Calculate Percentage

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args)
	{
		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the value: ");
		    double value = sc.nextDouble();
		    
		    System.out.println("Enter the total value: ");
		    double totalValue = sc.nextDouble();
		    
		    double percentage = (value/totalValue) * 100;
		    
		    System.out.println("Percentage is "+percentage);
		 
	}
}

Output

Enter the value: 92
Enter the total value: 120
Percentage is 76.66666666666667

How Does This Program Work ?

		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the value: ");
		    double value = sc.nextInt();
		    
		    System.out.println("Enter the total value: ");
		    double totalValue = sc.nextInt();

In this program, we have taken the input of the value and the total value to calculate the percentage using the Scanner class in Java.

		    double percentage = (value/totalValue) * 100;

Then, using the formula to calculate the percentage discussed above we have calculated the percentage.

   System.out.println("Percentage is "+percentage);

At last, we display the calculated Percentage.

This is the Java Program to Calculate Percentage. Now, let’s see a bonus Java program to calculate to Percentage of students.

Java program to calculate to Percentage of students

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args)
	{
		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the obtained marks: ");
		    double marks = sc.nextDouble();
		    
		    System.out.println("Enter the total marks: ");
		    double totalMarks = sc.nextDouble();
		    
		    double percentage = (marks/totalMarks) * 100;
		    
		    System.out.println("Percentage obtained by the student is "+percentage);
		 
	}
}

Output

Enter the obtained marks: 555
Enter the total marks: 600
Percentage obtained by the student is 92.5

This is the Java program to calculate to Percentage of students.

Conclusion

I hope after going through this post, you understand the Java Program to Calculate Percentage. We also learned to code the Java program to calculate to Percentage of students. If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *