
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Percentage in Java
Percent means percent (hundreds), i.e., a ratio of the parts out of 100. The symbol of a percent is %. We generally count the percentage of marks obtained, return on investment etc. The percentage can go beyond 100% also.
For Example, assuming that we have total and a part. So we say what part is what percent of total and should be calculated as −
percentage = ( part / total ) × 100
Algorithm
Below is the algorithm to calculate percentage in Java:
1. Collect values for part and total 2. Apply formula { percentage = ( part / total ) × 100 } 3. Display percentage
Example
import java.util.Scanner; public class Percentage { public static void main(String args[]){ float percentage; float total_marks; float scored; Scanner sc = new Scanner(System.in); System.out.println("Enter your marks ::"); scored = sc.nextFloat(); System.out.println("Enter total marks ::"); total_marks = sc.nextFloat(); percentage = (float)((scored / total_marks) * 100); System.out.println("Percentage ::"+ percentage); } }
Output
Enter your marks :: 500 Enter total marks :: 600 Percentage ::83.33333
Advertisements