PDF download Download Article PDF download Download Article

Calculating percentage can be of great help. But when the numbers get bigger, using a program to calculate it becomes much easier. Here is how you can create a program to calculate percentage, in Java.

  1. Plan your program. Though calculating percentage isn't difficult, it is always a good practice to plan your program before beginning to code. Try finding the answers of the following questions:[1]
    • Is your program going to handle large numbers? If yes, then try thinking about ways in which your program can handle large range of numbers. One way of doing this is using a float or long variable instead of int.
  2. Write the code. To calculate percentage, you will need two parameters:
    • The total score (or the maximum possible score); and,
    • The obtained score whose percentage you wish to calculate.
      • For example: If a student scores 30 marks out of 100 in a test, and you wish to calculate the percentage marks scored by the student, 100 is the total marks (or the maximum possible score). 30 is the obtained score whose percentage you wish to calculate.
    • The formula to calculate percentage is:

      Percentage = (Obtained score x 100) / Total Score
    • To get these parameters (inputs) from the user, try using the Scanner function in Java.
    Advertisement
  3. Use the formula given in the previous step to calculate the percentage. Make sure that the variable used for storing the value of percentage is of type float. If not, the answer may not be correct.
    • This is because, the float data-type is 32 bit single precision that even considers decimals in mathematical calculations. Thus, using a float variable, the answer for a mathematical calculation like 5 / 2 (5 divided by 2) will be 2.5
      • If the same calculation (5 / 2) if done using an int variable, the answer will be 2.
      • However, the variables in which you stored the total score and obtained score can be int. Using a float variable for the percentage will automatically convert the int to float; and the total calculation will be done in float instead of int.
  4. Once the program has calculated the percentage, display it to the user. Use the System.out.print or System.out.println (to print on a new line) function, in Java, for this.[2]
  5. Advertisement
Method 1
Method 1 of 1:

Sample Code

PDF download Download Article
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 + " %");
    }
}


Community Q&A

Search
Add New Question
  • Question
    How do I write 5.365 as a percentage?
    Community Answer
    Community Answer
    536.5% (original number times 100).
  • Question
    Should I use float instead to find percentages? (It's for an exam.)
    Community Answer
    Community Answer
    It really depends on the application. A float variable will give you more precise values with decimals, and an int variable will give you rounded whole numbers.
  • Question
    How do I get decimal points instead of zero?
    Community Answer
    Community Answer
    You use a float or double instead of int. A double is more accurate, but you won't necessarily use that more than a float.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Calculate PercentagesCalculate Percentages
Compile & Run Java Program Using Command PromptA Beginner’s Guide to Compiling & Running Java Programs
Check Your Java Version in the Windows Command LineUse Easy Windows CMD Commands to Check Your Java Version
Do Division in Java Do Division in Java (Integer and Floating Point)
Compile and Run Java Program by Notepad Compile and Run Java Programs Using Notepad++
Set Java Home Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Check Java Version on a Mac Quickly and Easily Check Java Version on a Mac
Check Null in JavaCheck Null in Java
Create an Executable File from EclipseCreate an Executable File from Eclipse
Add JARs to Project Build Paths in Eclipse (Java)Add JARs to Project Build Paths in Eclipse (Java)
Update JavaUpdate Java
Program in JavaProgram in Java
Create JAR File Create a JAR File With Eclipse
Print Double Quotes in JavaSimple Steps to Type a Bunny with Your Keyboard
Advertisement

About This Article

Jake Adams
Co-authored by:
Academic Tutor
This article was co-authored by Jake Adams. Jake Adams is an academic tutor and the owner of Simplifi EDU, a Santa Monica, California based online tutoring business offering learning resources and online tutors for academic subjects K-College, SAT & ACT prep, and college admissions applications. With over 14 years of professional tutoring experience, Jake is dedicated to providing his clients the very best online tutoring experience and access to a network of excellent undergraduate and graduate-level tutors from top colleges all over the nation. Jake holds a BS in International Business and Marketing from Pepperdine University. This article has been viewed 420,534 times.
How helpful is this?
Co-authors: 3
Updated: January 26, 2024
Views: 420,534
Categories: Java
Thanks to all authors for creating a page that has been read 420,534 times.

Reader Success Stories

  • Nayanz

    Nayanz

    Mar 8, 2017

    "This really helped me to know about the program of Java calculator."
Share your story

Is this article up to date?

Advertisement