Program to find compound interest
Last Updated :
29 Jul, 2024
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++
// CPP program to find compound interest for
// given values.
#include <bits/stdc++.h>
using namespace std;
int main()
{
double principal = 10000, rate = 5, time = 2;
/* Calculate compound interest */
double A = principal * (pow((1 + rate / 100), time));
double CI = A- principal;
cout << "Compound interest is " << CI;
return 0;
}
//This Code is Contributed by Sahil Rai.
C
// C program to calculate Compound Interest
#include <stdio.h>
#include<math.h> // for using pow function we must include math.h
int main() {
double principal = 10000; // principal amount
double rate = 5; //annual rate of interest
double time = 2; // time
// Calculating compound Interest
double Amount = principal * (pow((1 + rate / 100), time));
double CI = Amount - principal;
printf("Compound Interest is : %lf",CI);
return 0;
}
Java
// Java program to find compound interest for
// given values.
import java.io.*;
class GFG
{
public static void main(String args[])
{
double principal = 10000, rate = 5, time = 2;
/* Calculate compound interest */
double A = principal *
(Math.pow((1 + rate / 100), time));
double CI = A - principle;
System.out.println("Compound Interest is "+ CI);
}
}
//This Code is Contributed by Sahil Rai.
Python3
# Python3 program to find compound
# interest for given values.
def compound_interest(principal, rate, time):
# Calculates compound interest
A = principal * (pow((1 + rate / 100), time))
CI= A - principal
print("Compound interest is", CI)
compound_interest(10000, 5, 2)
#This Code is Contributed by Sahil Rai.
C#
// C# program to find compound
// interest for given values
using System;
class GFG {
// Driver Code
public static void Main()
{
double principal = 10000, rate = 5, time = 2;
// Calculate compound interest
double A = principal * (Math.Pow((1 +
rate / 100), time));
double CI = A - principal;
Console.Write("Compound Interest is "+ CI);
}
}
//This Code is Contributed by Sahil Rai.
JavaScript
<script>
// JavaScript program to
// find compound interest for
// given values.
let principal = 10000, rate = 5,
time = 2;
/* Calculate compound interest */
let A = principal *
(Math.pow((1 + rate / 100), time));
let CI = A - principal;
document.write("Compound interest is " + CI);
</script>
// This Code is Contributed by Sahil Rai.
PHP
<?php
// PHP program to find
// compound interest for
// given values.
$principal = 10000;
$rate = 5;
$time = 2;
// Calculate compound interest
$A = $principal * (pow((1 +
$rate / 100), $time));
$CI = $A - $principal;
echo("Compound interest is " . $CI);
?>
Output:
Compound interest is 1025
Time complexity: O(1), as there is no loop used so constant time
Auxiliary Space: O(1)
Similar Reads
Program to find simple interest Given Principal p, Rate r and Time t, the task is to calculate Simple Interest.Examples :Input: p = 10000, r = 5, t = 5 Output:2500 Explanation: We need to find simple interest on Rs. 10,000 at the rate of 5% for 5 units of time. Input: p = 3000, r = 7, t = 1 Output:210The basic idea is to calculate
2 min read
Program to find Simple Interest and Compound Interest in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given principal(p), rate(r), time(t), the task is to ca
2 min read
Compound Interest | Set-2 Compound Interest is one of the most important topics in Quantitative Aptitude and is frequently asked in competitive exams in India. Pre-requisites: Compound Interest - Formula, Definition and ExamplesReal-Life Applications of Compound InterestCompound Interest - Aptitude Questions and AnswersAlso,
5 min read
Program to find the rate percentage from compound interest of consecutive years Given two integers N1 and N2 which is the Compound Interest of two consecutive years. The task is to calculate the rate percentage.Examples: Input: N1 = 660, N2 = 720 Output: 9.09091 %Input: N1 = 100, N2 = 120 Output: 20 % Approach: The rate percentage can be calculated with the formula ((N2 - N1) *
3 min read
Program to find the Depreciation of Value The value of any article or item subject to wear and tear, decreases with time. This decrease is called its Depreciation. Given three variable V1, R and T where V1 is the initial value, R is the rate of depreciation and T is the time in years. The task is to find the value of the item after T years.
3 min read