How to Calculate a Percentage with Java: 4 Steps

Table of contents:

How to Calculate a Percentage with Java: 4 Steps
How to Calculate a Percentage with Java: 4 Steps
Anonim

Calculating percentages can be very helpful. When the numbers are large, using a program to calculate them greatly simplifies the operation. Here's how to create a program to calculate percentages in Java.

Steps

Calculate Percentage in Java Step 1
Calculate Percentage in Java Step 1

Step 1. Plan your schedule

Calculating a percentage isn't difficult, but it's always a good idea to plan your program before you start coding. Try to find answers to the following questions:

Will your program have to handle large numbers? If so, try to think of ways to make your program handle large ranges of numbers. One way to do this is to use a "float" or "long" variable instead of "int"

Calculate Percentage in Java Step 2
Calculate Percentage in Java Step 2

Step 2. Write the code

To calculate a percentage, you will need to have two parameters:

  • The total score (or the maximum possible value)
  • The score obtained whose percentage is what you want to calculate.

    For example: if a student scores 30 points out of 100 on a test, and you want to calculate the percentage score the student gets, 100 is the maximum possible value. 30 is the score obtained whose percentage is what you want to calculate.

  • The formula for calculating the percentage is:

    Percentage = (Score x 100) / Total score.

  • To get the parameters (input) from the user, try using Java's "Scanner" function.
Calculate Percentage in Java Step 3
Calculate Percentage in Java Step 3

Step 3. Calculate the percentage

Use the formula given in the previous step to calculate the percentage. Make sure the variable used to store the percentage value is of type float. If you don't, the result may be incorrect.

  • This is because, the float data type is a single-precision 32-bit format that considers decimals in mathematical calculations. Consequently, using a float variable, the answer for a mathematical calculation such as 5 divided by 2 will be 2.5.

    • If you did the same calculation as 5 divided by 2 using an "int" variable, the answer would be 2.
    • The variables in which you will store the "total score" and "score obtained" values can be "int" instead. Using a "float" variable for "percent" will automatically convert "int" values to "float"; the total calculation will be performed in float and not in int.
    Calculate Percentage in Java Step 4
    Calculate Percentage in Java Step 4

    Step 4. Show the percentage to the user

    When the program has calculated the sum, it shows it to the user. Use the Java functions System.out.print or System.out.println (to print on a new line) to do this.

    Code Example

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

    Advice

    • Try creating a GUI, which will make the program more interactive and easier to use.
    • Try expanding your program to other math operations.

Recommended: