# How to calculate percentage in Java: 4 steps

Calculating percentages can be a big help. But when the numbers get large, it will be much easier to use a program to calculate it. For your attention, an instruction on how you can create a program to calculate the percentage in Java.

## Steps

#### Step 1. Create an algorithm for your program

Although calculating percentages is not difficult, it is best if you write the algorithm for the program before you start writing the actual code. Try to find answers to the following questions:

### Will your program handle large numbers? If so, try to think about ways in which your program can handle a large range of numbers. One way to do this is to use a float or long variable instead of int

#### Step 2. Write the code

To calculate the percentage, you need two parameters:

• Overall score (or the highest possible score); and
• score receivedthe percentage of which you want to calculate.

### For example: if a student scored 30 points out of 100 possible in the test, and you want to calculate the percentage of points scored by the student - 100 is the total score (or the maximum possible score), 30 is the resulting grade, the percentage of which you want to calculate

• The formula for calculating interest is:

Percentage = (Score Received x 100) / Total Score

• To get these parameters (input) from the user, try using the Scanner function in Java.

#### Step 3. Calculate the percentage

Use the formula from the previous step to calculate the percentage. Make sure the variable used to store the percent value is of type "float". If not, then the answer may not be correct.

• This is because the float datatype (32-bit single precision floating point numbers) takes into account the "decimals" numeric datatype in mathematical calculations. Thus, using a "float" variable, the answer for a mathematical calculation, for example 5/2 (5 divided by 2) would be 2, 5.

• If we calculate (5/2) using the int variable, then the answer will be 2.
• However, the variables in which you store the total score and the resulting score can be of type int. Using a float variable for percent will automatically convert int to float. And the general calculation will be done in "float" instead of "int".

#### Step 4. Display the percentage value for the user

After the program has calculated the percentage, display it to the user. To do this in Java, use the System.out.print or System.out.println functions (to print / output on a new line).

### Method 1 of 1: Sample Code

```import java.util. Scanner; public class main_class {public static void main (String [] args) {int total, score; float percentage; Scanner inputNumScanner = new Scanner (System.in); System.out.println ("Enter the total, or max, score:"); total = inputNumScanner.nextInt (); System.out.println ("Enter the score obtained:"); score = inputNumScanner.nextInt (); percentage = (score * 100 / total); System.out.println ("The percentage is =" + percentage + "%"); }}

```