Java calculate percentage

Hey guys!! Welcome to flower brackets blog. OK, today we going to talk about java calculate percentage.

java calculate percentage

Well, the word percentage was familiar when we were in tenth and twelfth grades. Calculating percentage was very crucial and important to get scholarships in top universities.

Also read – perimeter of rectangle in java

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

percentage = ( a / b ) * 100

Here,
a = marks obtained
b = total marks

Well, now we will see java program to calculate percentage. So, let’s dig in,

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

Related Posts