Calculating percentages in Java

Author: John Pratt
Date Of Creation: 16 April 2021
Update Date: 1 July 2024
Anonim
Exercise: Program to Calculate Percentage in Java
Video: Exercise: Program to Calculate Percentage in Java

Content

Calculating percentages has many applications. But as the numbers get bigger, it's easier to use a program for this. Below is explained how to write a program in Java for calculating percentages.

To step

  1. Plan your program. Although calculating a percentage is not that difficult, it is always wise to make a schedule of your program before programming. Try to answer the following questions:
    • Will your program work with large numbers? If so, look for ways to make your program deal with many different types of numbers. One way to do this is with type float or lung as a variable, instead of int (integer).
  2. Write the code. To calculate the percentage you need two parameters:
    • The total score (or the maximum possible score).
    • The achieved score (of which you want to calculate the percentage).
      • For example: If a student gets 30 of the 100 questions correct on a test, and you want to calculate the percentage, then 100 is the total (the maximum score) and 30 is the score obtained, which you will convert to a percentage.
    • The formula for calculating the percentage is:

      Percentage = (Achieved Score x 100) / Total Score
    • To get these parameters (input) in Java from the user, you can use the Scannerfunction.
  3. Calculate the percentage. Use the formula as indicated in the previous step to calculate the percentage. Make sure the variable you are using to store the value of the percentage, has the float type. If not, the answer may be incorrect.
    • This is because the floatdata type has a precision of 32 bit that even takes decimals into account in mathematical calculations. So the answer to a math calculation such as 5/2 (5 divided by 2) with type float will be 2.5
      • The same calculation (5/2) with the type int for the variable, returns 2.
      • The variables you use to store the total score and the achieved score can, however int to be. By a float to be used as the type for the variable for the percentage shall int automatically to a float be converted; the total calculation will then be performed as a float instead of an integer.
  4. Show the percentage to the user. Once the program has calculated the percentage, show it to the user. Use the function for this System.out.print or System.out.println (to print on a new line) in Java.

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

Tips

  • Try to create a graphical interface (GUI) to make the program more interactive and easier to use.
  • Try to expand your program so that you can perform multiple calculations with it.