Compound Interest
Given separate whole-integer arguments principal, rate and years, return the interest earned under compound interest as canonical ASCII decimal text with…
Given separate whole-integer arguments principal, rate and years, return the interest earned under compound interest as canonical ASCII decimal text with exactly two fractional digits. Principal is in whole abstract currency units; rate is a whole annual percentage. Return earned interest, excluding the original principal. Use annual compounding and round half up only once at the end; do not use binary floating point.
0 <= principal <= 100000 0 <= rate <= 10 0 <= years <= 5 All inputs are exact integers; booleans and non-integers are invalid. No fractional principal, rate or years. Output has no symbol, grouping, whitespace or exponent. Use BigInt in JavaScript; Java long and C++ long long hold all declared intermediates.
Example 1: input principal = 1000, rate = 5, years = 2; output "102.50". After two years, 1000 × 1.05 × 1.05 = 1102.50; subtracting principal gives 102.50 earned interest.
Example 2: input principal = 2, rate = 5, years = 2; output "0.21". Exact earned interest is 2 × 1.05 × 1.05 - 2 = 0.205; the half-cent tie rounds up once to 0.21.
Example 3: input principal = 3, rate = 5, years = 3; output "0.47". Exact earned interest is 3 × 1.05 × 1.05 × 1.05 - 3 = 0.472875; final-only rounding gives 0.47.
Category: Math & Numbers. Topic: math. Difficulty: Easy. Patterns: math.