Given four integers p, r, t, and n representing the principal amount, annual interest rate (in percentage), time period (in years), and the number of compounding periods per year respectively.
Let's the final amount accumulated after t years using the compound interest formula:
Return the floor value of the accumulated amount A.
Examples:
Input: p = 1000, t = 2, n = 2, r = 10
Output: 1215
Explanation: After applying compound interest for 2 years at an annual rate of 10, compounded twice per year, the accumulated amount becomes 1215.50625. Taking its floor value gives 1215.Input: p = 100, t = 1, n = 1, r = 10
Output: 110
Explanation: After applying compound interest for 1 year at an annual rate of 10, compounded once per year, the accumulated amount becomes 110.
Using Compound Interest Formula - O(1) Time and O(1) Space
#include <iostream>
#include <cmath>
using namespace std;
int calculateFutureValue(int p, int t, int n, int r) {
return (int)(p * pow(1.0 + (double)r / (100 * n), n * t));
}
int main() {
int p = 1000;
int t = 2;
int n = 2;
int r = 10;
cout << calculateFutureValue(p, t, n, r);
return 0;
}
import java.lang.Math;
public class GFG {
public static int calculateFutureValue(int p, int t, int n, int r) {
return (int)(p * Math.pow(1.0 + (double)r / (100 * n), n * t));
}
public static void main(String[] args) {
int p = 1000;
int t = 2;
int n = 2;
int r = 10;
System.out.println(calculateFutureValue(p, t, n, r));
}
}
import math
def calculateFutureValue(p, t, n, r):
return int(p * math.pow(1.0 + r / (100 * n), n * t))
if __name__ == '__main__':
p = 1000
t = 2
n = 2
r = 10
print(calculateFutureValue(p, t, n, r))
using System;
public class GFG {
public static int calculateFutureValue(int p, int t, int n, int r) {
return (int)(p * Math.Pow(1.0 + (double)r / (100 * n), n * t));
}
public static void Main() {
int p = 1000;
int t = 2;
int n = 2;
int r = 10;
Console.WriteLine(calculateFutureValue(p, t, n, r));
}
}
// Re
function calculateFutureValue(p, t, n, r) {
return Math.floor(p * Math.pow(1.0 + r / (100 * n), n * t));
}
// Driver code
let p = 1000;
let t = 2;
let n = 2;
let r = 10;
console.log(calculateFutureValue(p, t, n, r));