Calculating percentage in Java
In this article, you will learn how to calculate the percentage of a total using the Java programming language. Such a type of logical query can be asked in a programming interview or need to be implemented during some application development. In some cases, when there is a need to find the ratio or portion of a quantity as a part of another quantity, you should communicate it as a percentage. This exercise will basically simplify the essential function that we will use to have the option to calculate a percentage.
Calculate Percentage
A percentage can be calculated by dividing the value by the total value, and then multiplying the result by 100. The formula used to calculate percentage-
Percentage = (value/total value)×100%
Java program to calculate percentage
import java.util.Scanner;
public class CalculatePercentage {
public static void main(String args[]){
float percentage;
float totalmarks;
float scored;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks : ");
scored = sc.nextFloat();
System.out.println("Enter total marks : ");
totalmarks = sc.nextFloat();
percentage = (float)((scored / totalmarks) * 100);
System.out.println("Percentage : "+ percentage);
}
}
Output of the above code:
Enter your marks : 375
Enter total marks : 400
Percentage : 93.75
Related Articles
Enum from string JavaJava enum
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java