Java Program to Calculate the Percentage of a Number

Percentage

Java programs to calculate the percentage of a number have been given below. A percentage is represented as a number or a ratio which is a fraction of 100. It is denoted by the symbol '%'. For example 50% represents (50 / 100) or 0.50. So, 50% of a number b will be calculated as b * (50 / 100) or b * 0.50.


Suppose a and b are two numbers. Then, the solution to the percentage problem is obtained by answering any of the following queries.

(i) What is a% of b?

(ii) a is what % of b?

(iii) a is b% of what?


Let's assume a = 15 and b = 60, then the solutions w.r.t the above queries:

(i) ___ is a% of b := 15% of 60 := 60 * (15 / 100) := 9

(ii) a is ___ % of b := (15 / 60) * 100 := 25%

(iii) a is b% of ___ := (15 / 60) * 100 := 25


In the following section, Java programs to solve the above problems are given along with the algorithm, pseudocode and time-complexity of the programs.





1. Algorithm to calculate percentage of a number


1. Take a number a as input.

2. Take another number b as input where b% of a is to be calculated.

3. Calculate a * (b / 100) and declare it as the output.




2. Pseudocode to calculate percentage of a number


Input : Two numbers a, b

Output : b% of a

1. Procedure percentage(a, b):

2. x := a * (b / 100)

3. Return x

4. End Procedure





3. Time complexity to calculate percentage of a number


Time Complexity: O(1)




4. Programs to calculate percentage




4.1. Java Program to calculate "What is a% of b?"

Code has been copied
/*****************************************
	     alphabetacoder.com
Java program to calculate what is a% of b? 
******************************************/

import java.util.Scanner;
import java.text.DecimalFormat;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // Creating an object of DecimalFormat class
        // Round upto 3 decimal places
        DecimalFormat obj = new DecimalFormat("#.###");

        // declare variables
        double a, b, x;

        // take inputs
        System.out.println("Enter values to calculate a% of b");
        System.out.print("Enter a = ");
        a = sc.nextFloat();
        System.out.print("Enter b = ");
        b = sc.nextFloat();

        // calculate value
        x = b * (a / 100);

        // display result upto 3 decimal places
        System.out.println(obj.format(x) + " is " + obj.format(a) + "% of " + obj.format(b));
    }
}

Output


Enter values to calculate a% of b

Enter a = 85

Enter b = 125

106.25 is 85% of 125





4.2. Java Program to calculate "a is what % of b?"

Code has been copied
/*******************************************
	      alphabetacoder.com
Java program to calculate a is what % of b? 
********************************************/

import java.util.Scanner;
import java.text.DecimalFormat;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // Creating an object of DecimalFormat class
        // Round upto 3 decimal places
        DecimalFormat obj = new DecimalFormat("#.###");

        // declare variables
        double a, b, x;

        // take inputs
        System.out.println("Enter values to calculate a is what % of b?");
        System.out.print("Enter a = ");
        a = sc.nextFloat();
        System.out.print("Enter b = ");
        b = sc.nextFloat();

        // calculate value
        x = (a / b) * 100;

        // display result upto 3 decimal places
        System.out.println(obj.format(a) + " is " + obj.format(x) + "% of " + obj.format(b));
    }
}

Output


Enter values to calculate a is what % of b?

Enter a = 10

Enter b = 30

10 is 33.333% of 30





4.3. Java Program to calculate "a is b% of what?"

Code has been copied
/******************************************
             alphabetacoder.com
Java program to calculate a is b% of what? 
*******************************************/

import java.util.Scanner;
import java.text.DecimalFormat;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // Creating an object of DecimalFormat class
        // Round upto 3 decimal places
        DecimalFormat obj = new DecimalFormat("#.###");

        // declare variables
        double a, b, x;

        // take inputs
        System.out.println("Enter values to calculate a is b % of what?");
        System.out.print("Enter a = ");
        a = sc.nextFloat();
        System.out.print("Enter b = ");
        b = sc.nextFloat();

        // calculate value
        x = (a / b) * 100;

        // display result upto 3 decimal places
        System.out.println(obj.format(a) + " is " + obj.format(b) + "% of " + obj.format(x));
    }
}

Output


Enter values to calculate a is b % of what?

Enter a = 27

Enter b = 81

27 is 81% of 33.333