What is ‘Compound interest’ ?
Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest. It is the result of reinvesting interest, rather than paying it out, so that interest in the next period is then earned on the principal sum plus previously-accumulated interest. Compound interest is standard in finance and economics.
Compound interest may be contrasted with simple interest, where interest is not added to the principal, so there is no compounding.
Compound Interest formula:
Formula: to calculate compound interest annually is given by:
Amount= P(1 + R/100)t
Compound Interest = Amount – P
Where,
P is the principal amount
R is the rate and
T is the time span
Pseudo Code:
Input principal amount. Store it in some variable say principal
Input time in some variable say time.
Input rate in some variable say rate.
Calculate Amount using the formula,
Amount = principal* (1 + rate / 100) time).
Calculate Compound Interest using Formula.
Finally, print the resultant value of CI.
Example:
Input: Principal (amount): 1200, Time: 2, Rate: 5.4
Output: Compound Interest = 133.099243
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
double principal = 10000, rate = 5, time = 2;
double A = principal * ( pow ((1 + rate / 100), time ));
double CI = A- principal;
cout << "Compound interest is " << CI;
return 0;
}
|
C
#include <stdio.h>
#include<math.h> // for using pow function we must include math.h
int main() {
double principal = 10000;
double rate = 5;
double time = 2;
double Amount = principal * ( pow ((1 + rate / 100), time ));
double CI = Amount - principal;
printf ( "Compound Interest is : %lf" ,CI);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void main(String args[])
{
double principal = 10000 , rate = 5 , time = 2 ;
double A = principal *
(Math.pow(( 1 + rate / 100 ), time));
double CI = A - principle;
System.out.println( "Compound Interest is " + CI);
}
}
|
Python3
def compound_interest(principal, rate, time):
A = principal * ( pow (( 1 + rate / 100 ), time))
CI = A - principal
print ( "Compound interest is" , CI)
compound_interest( 10000 , 5 , 2 )
|
C#
using System;
class GFG {
public static void Main()
{
double principal = 10000, rate = 5, time = 2;
double A = principal * (Math.Pow((1 +
rate / 100), time));
double CI = A - principal;
Console.Write( "Compound Interest is " + CI);
}
}
|
PHP
<?php
$principal = 10000;
$rate = 5;
$time = 2;
$A = $principal * (pow((1 +
$rate / 100), $time ));
$CI = $A - $principal ;
echo ( "Compound interest is " . $CI );
?>
|
Javascript
<script>
let principal = 10000, rate = 5,
time = 2;
let A = principal *
(Math.pow((1 + rate / 100), time));
let CI = A - principal;
document.write( "Compound interest is " + CI);
</script>
|
Output:
Compound interest is 1025
Time complexity: O(1), as there is no loop used so constant time
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!