Calculate increase or decrease percentage in Java

What is percentage?

In mathematics , Percentage is a number or ratio expressed as a fraction of 100. If we calculate the part of whole number and multiply the result by 100, we will get the percentage value. It is denoted by percent (%) sign.

Percentage = (part/whole) x 100

Example : A student scored 120 marks out of 150 and the percentage is calculated as (120/150) * 100 = 80 %

Increase or Decrease percentage

In some cases, we want to compare the previous value and current value to check whether it is increased or decreased. Lets look at the formula to calculate this difference in the form of percentage.

Increase or decrease percentage = (New value – Old value) / Old value x 100

If the new value is greater than the old value, we will get the the increase percentage as a positive number.

If the new value is lesser than the old value, we will get the decrease percentage as a negative number.

Java program to calculate the increase or decrease percentage

Lets consider Stock market as a example where each stock price is changed every day. The stock market using the percentage notation to show whether the particular stock is increased or decreased. If we compare the stock price between the previous day and current day, we can get that percentage value.

Percentage calculation in Java
Percentage calculation in Java

In this example, we are going to write a java program to calculate the increase or decrease percentage value for stocks. This is a command line program in which user needs to provide the input for yesterday and todays stock price. Based on these values, we are calculating the percentage value.

Output

Enter Yesterday Stock Price :
75
Enter Today’s Stock Price :
105
Price Difference : 30.0
Percentage Growth : 40.00%