519.Calculate Simple Interest
Easy
Given a
principal amount, an annual interest rate in percent, and a number of years, return the simple interest earned, formatted with two decimal places.
Simple interestSimple InterestInterest charged only on the original amount: principal × rate × years / 100. It does not compound. grows by the same amount every year, so the formula is a single line. The interesting part is choosing variable types. The product can reach five billion — too big for an int — and money should not pick up rounding errors. The first solution uses a double; the second keeps everything as whole numbers by counting in hundredths, which is exact.
Example 1:
Input: principal = 1000, rate = 5, years = 2
Output: "100.00"
Example 2:
Input: principal = 1234, rate = 7, years = 3
Output: "259.14"
+ 6 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ principal ≤ 1000000 - ●
1 ≤ rate ≤ 100 - ●
1 ≤ years ≤ 50
principal =
1000
rate =
5
years =
2