How to Calculate Percentages in Java: 4 Steps (with Pictures)

Table of contents:

How to Calculate Percentages in Java: 4 Steps (with Pictures)
How to Calculate Percentages in Java: 4 Steps (with Pictures)

Video: How to Calculate Percentages in Java: 4 Steps (with Pictures)

Video: How to Calculate Percentages in Java: 4 Steps (with Pictures)
Video: How to shoot a knuckleball | Learn CR7 free kick 2024, December
Anonim

This article on how to calculate percentages can be very useful. However, as the numbers get bigger, using a program to calculate them will greatly simplify your task. Here's how to make a program to calculate percentages, using the Java programming language.

Step

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

Step 1. Plan your program

While calculating percentages isn't difficult, it's always a good practice to plan your program before you start writing code. Answer the following questions:

Will your program handle large numbers of numbers? If so, then think about how your program can handle a large range of numbers. One way is to use float or long variables instead of int

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

Step 2. Write down the code

To calculate the percentage, you need two parameters:

  • Total value (or maximum likelihood value); and,
  • Value earned whose percentage you want to calculate.

    For example: If a student scores 30 out of 100 on an exam, and you want to calculate the percentage score that student got, 100 is the total score (or the maximum possible score) and 30 is the score for which you will calculate the percentage

  • The formula for calculating the percentage is:

    Percentage = (Obtained value x 100) / Total value

  • To get this parameter (input) from the user, use the Scanner function in Java.
Calculate Percentage in Java Step 3
Calculate Percentage in Java Step 3

Step 3. Calculate the percentage

Use the formula in the previous step to calculate the percentage. Make sure the variable used to store the percentage value is of type float. Otherwise, the answer may not be correct.

  • This is because, the float data type has a single precision of 32 bits so it takes decimal numbers into account in mathematical calculations. Thus, using the float variable, the answer to a mathematical calculation like 5 / 2 (5 divide 2) is 2.5.

    • If the same calculation (5 / 2) is performed using the int variable, the answer is 2.
    • However, the variable where you store the total value and the return value can be an int. Using a float variable for a percentage will automatically convert an int to a float; and the total calculation will be done in float variable not int.
Calculate Percentage in Java Step 4
Calculate Percentage in Java Step 4

Step 4. Display the percentage to the user

After the program calculates the percentage, display the result to the user. Use the System.out.print or System.out.println functions (to print a new line), in Java.

Method 1 of 1: Code Example

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 total or maximum value: "); total = inputNumScanner.nextInt(); System.out.println("Enter value: "); score = inputNumScanner.nextInt(); percentage = (score * 100/ total); System.out.println("Percentage is = " + percentage + " %"); } }

Tips

  • Create a GUI or graphical user interface, which will make the program much more interactive and easier to use.
  • Extend your program to perform some mathematical calculations.

Recommended: