Java program to calculate the percentage



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
Updated on: 2024-06-14T13:37:08+05:30

39K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements