diff --git a/src/main/java/com/williamfiset/algorithms/math/ReturnOnInvestment.java b/src/main/java/com/williamfiset/algorithms/math/ReturnOnInvestment.java new file mode 100644 index 000000000..a6e1b72ce --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/math/ReturnOnInvestment.java @@ -0,0 +1,29 @@ +/** + * Calculates Return on Investment (ROI) as a percentage. + * + *
ROI measures the profitability of an investment relative to its cost. + * + *
Formula: ROI = (Gain - Cost) / Cost × 100 + * + *
Reference: https://www.investopedia.com/terms/r/returnoninvestment.asp + * + * @author InukaWijerathna + */ +package com.williamfiset.algorithms.math; + +public class ReturnOnInvestment { + + public static double roi(double gainFromInvestment, double costOfInvestment) { + if (costOfInvestment <= 0) { + throw new IllegalArgumentException("costOfInvestment must be greater than 0"); + } + return (gainFromInvestment - costOfInvestment) / costOfInvestment * 100.0; + } + + public static void main(String[] args) { + System.out.println(roi(1000, 500)); // 100.0 + System.out.println(roi(500, 500)); // 0.0 + System.out.println(roi(200, 500)); // -60.0 + System.out.println(roi(0, 500)); // -100.0 + } +}