Calculate percentage in java

In this post we are going to learn calculate percentage in java.

calculate percentage in java

Calculate percentage in java

How to calculate percentage in java is one of the most common java interview question.

Also read – perimeter of rectangle in java

Basically percentage is a number expressed as fraction of 100 and denoted using percent sign (%). Formula is,

percentage = ( a / b ) * 100

Here,

a = marks obtained

b = total marks


Let’s see java program on how to calculate percentage in java,

import java.util.Scanner;

public class CalculatePercentageDemo
{
   public static void main(String[] args)
   {
      float percentage;
      float total;
      float marksObtained;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter marks obtained : ");
      marksObtained = sc.nextFloat();
      System.out.println("Please enter total : ");
      total = sc.nextFloat();
      percentage = (float)((marksObtained / total) * 100);
      System.out.println("Percentage : " + percentage);
      sc.close();
   }
}

Output:

Please enter marks obtained : 594
Please enter total : 600
Percentage : 99.0