Calculate percentage java – Java Program to Calculate Exact Percentage

Calculate percentage java: In the previous article, we have seen Java Program to Generate a Random Number

In this article we will see how we can compute exact percentage of something by using Java programming language.

Java Program to Calculate Exact Percent

Percentage:

Java calculate percentage: Percentage refers to the fraction of 100.

Formula of Percentage = (current value/ total value) * 100

Example:

current value =150
total value = 300
Percentage = (150/300)*100 = 50%

Let’s see different ways to compute percentage.

Method-1: Java Program to Calculate Exact Percentage By Using Static Input Value

Approach:

  • Declare current value and total value.
  • Then find percentage using the formula Percentage = (current value/ total value) * 100
  • Print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
       //Scanner class object created
       Scanner sc = new Scanner(System.in);
       
       //variables declared
       double totalValue, currentValue; 

       //total value declared       
       totalValue = 1000;
       //current value declared
       currentValue = 800;

       //computing percentage using formula
       double percent = (currentValue / totalValue)*100;
       //printing result
       System.out.println("Percentage = " + percent + " %");
    }
}
Output:

Percentage = 80.0 %

Method-2: Java Program to Calculate Exact Percentage By Using User Input Value

Approach:

  • Take user input of current value and total value.
  • Then find percentage using the formula Percentage = (current value/ total value) * 100
  • Print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
       //Scanner class object created
       Scanner sc = new Scanner(System.in);
       
       //variables declared
       double totalValue, currentValue; 
       double percent;

       //taking user input of total value
       System.out.println("Enter total value: ");       
       totalValue = sc.nextInt();
       //taking user input of current value
       System.out.println("Enter current value: ");
       currentValue = sc.nextInt();

       //computing percentage using formula
       percent = (currentValue / totalValue)*100;
       //printing result
       System.out.println("Percentage = " + percent + " %");
    }
}
Output:
Enter total value: 
600
Enter current value: 
432
Percentage = 72.0 %

Method-3: Java Program to Calculate Exact Percentage By Using User Defined Method

Approach:

  • Take user input of current value and total value.
  • Call a user defined method by passing both the values as parameter.
  • Inside method find percentage using the formula Percentage = (current value/ total value) * 100
  • Print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
       //Scanner class object created
       Scanner sc = new Scanner(System.in);
       
       //variables declared
       double totalValue, currentValue; 

       //taking user input of total value
       System.out.println("Enter total value: ");       
       totalValue = sc.nextInt();
       //taking user input of current value
       System.out.println("Enter current value: ");
       currentValue = sc.nextInt();
       //calling user defined findPercent() method
       findPercent(totalValue, currentValue);
    }
    
    //findPercent() method to find percentage
    public static void findPercent(double totalValue, double currentValue)
    {
       //computing percentage using formula
       double percent = (currentValue / totalValue)*100;
       //printing result
       System.out.println("Percentage = " + percent + " %");
    }
}
Output:

Enter total value: 
600
Enter current value: 
452
Percentage = 75.33333333333333 %

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Related Java Programs: