Calculate percentage in java

Let’s learn how do you calculate a percentage in java?

Calculate percentage in java

Calculate percentage of a student is one of the most common interview question.

calculate percentage in java

To calculate percentage of a student we have to divide obtained marks of student by total marks.

Basically percentage is a number expressed as fraction of 100 and denoted using percent sign “%”. Formula to calculate percentage is,

percentage = ( a / b ) * 100


a = marks obtained

b = total marks

Let’s see program to calculate percentage of marks obtained by a student.

// java calculate percentage
import java.util.Scanner;
public class CalculatePercentageDemo
   public static void main(String[] args)
      float percentage;
      float total;
      float marksObtained;
      Scanner sc = new Scanner(;
      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);


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

Also read – static keyword in java