C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Calculate Compound Annual Growth Rate (CAGR)

CAGR Formula

 Java Programs

Where:

  • Initial Value = Beginning investment value
  • Final Value = Ending investment value
  • n = Number of years

import java.util.Scanner;

 

public class CAGRCalculator {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter initial investment value: ");

        double initialValue = sc.nextDouble();

 

        System.out.print("Enter final investment value: ");

        double finalValue = sc.nextDouble();

 

        System.out.print("Enter number of years: ");

        int years = sc.nextInt();

 

        // CAGR calculation using Math.pow()

        double cagr = Math.pow((finalValue / initialValue), 1.0 / years) - 1;

 

        System.out.println("\n------ CAGR Calculation ------");

        System.out.printf("Initial Value : %.2f%n", initialValue);

        System.out.printf("Final Value   : %.2f%n", finalValue);

        System.out.printf("Time Period   : %d years%n", years);

        System.out.printf("CAGR          : %.2f%%%n", cagr * 100);

 

        sc.close();

    }

}

Output

INPUT :
Enter initial investment value: 100000
Enter final investment value: 180000
Enter number of years: 5

OUTPUT :
------ CAGR Calculation ------
Initial Value : 100000.00
Final Value   : 180000.00
Time Period   : 5 years
CAGR          : 12.48%

Explanation

1. Input Values

double initialValue = sc.nextDouble();

double finalValue = sc.nextDouble();

int years = sc.nextInt();

  • Reads investment values and time period.

2. CAGR Formula Implementation

double cagr = Math.pow((finalValue / initialValue), 1.0 / years) - 1;

Breakdown:

  • finalValue / initialValue → total growth ratio
  • 1.0 / years → annualized exponent
  • Math.pow() → calculates power
  • - 1 → converts growth factor to rate

3. Percentage Conversion

cagr * 100

  • Converts decimal value into percentage.

4. Formatted Output

System.out.printf("CAGR : %.2f%%%n", cagr * 100);

  • Displays CAGR up to 2 decimal places.