# How to calculate simple and compound interest

Mathematics is an integral part of programming. If you cannot solve simple problems in this area, you will have a much harder time than you need to.

Fortunately, learning how to do it is not too difficult. In this article, you will learn how to calculate simple and compound interest using Python, C ++, and JavaScript.

How do you calculate simple interest?

Simple interest is a method of calculating the amount of interest charged on a principal amount at a given rate and for a given period of time. You can calculate simple interest using the following formula:

``Simple Interest = (P x R x T)/100 Where, P = Principle AmountR = RateT = Time``

### The problem statement

we give you principal amount, interest rate, and time. You need to calculate and print the simple interest for the given values. Example: Either principle = 1000, rate = 7 and timePeriod = 2. Simple interest = (principle * rate * timePeriod) / 100 = (1000 * 7 * 2) / 100 = 140. Thus, the output is 140.

### The C ++ program to calculate simple interest

Here is the C ++ program to calculate simple interest:

``// C++ program to calculate simple interest// for given principle amount, time, and rate of interest.#include using namespace std;// Function to calculate simple interestfloat calculateSimpleInterest(float principle, float rate, float timePeriod){return (principle * rate * timePeriod) / 100;}int main(){float principle1 = 1000;float rate1 = 7;float timePeriod1 = 2;cout << "Test case: 1" << endl;cout << "Principle amount: " << principle1 << endl;cout << "Rate of interest: " << rate1 << endl;cout << "Time period: " << timePeriod1 << endl;cout << "Simple Interest: " << calculateSimpleInterest(principle1, rate1, timePeriod1) << endl;float principle2 = 5000;float rate2 = 5;float timePeriod2 = 1;cout << "Test case: 2" << endl;cout << "Principle amount: " << principle2 << endl;cout << "Rate of interest: " << rate2 << endl;cout << "Time period: " << timePeriod2 << endl;cout << "Simple Interest: " << calculateSimpleInterest(principle2, rate2, timePeriod2) << endl;float principle3 = 5800;float rate3 = 4;float timePeriod3 = 6;cout << "Test case: 3" << endl;cout << "Principle amount: " << principle3 << endl;cout << "Rate of interest: " << rate3 << endl;cout << "Time period: " << timePeriod3 << endl;cout << "Simple Interest: " << calculateSimpleInterest(principle3, rate3, timePeriod3) << endl;return 0;}``

Go out:

``Test case: 1Principle amount: 1000Rate of interest: 7Time period: 2Simple Interest: 140Test case: 2Principle amount: 5000Rate of interest: 5Time period: 1Simple Interest: 250Test case: 3Principle amount: 5800Rate of interest: 4Time period: 6Simple Interest: 1392``

Related: How to Find All the Factors of a Natural Number in C ++, Python, and JavaScript

### The Python program to calculate simple interest

Below is the Python program to calculate simple interest:

``# Python program to calculate simple interest# for given principle amount, time, and rate of interest.# Function to calculate simple interestdef calculateSimpleInterest(principle, rate, timePeriod):return (principle * rate * timePeriod) / 100principle1 = 1000rate1 = 7timePeriod1 = 2print("Test case: 1")print("Principle amount:", principle1)print("Rate of interest:", rate1)print("Time period:", timePeriod1)print("Simple Interest:", calculateSimpleInterest(principle1, rate1, timePeriod1))principle2 = 5000rate2 = 5timePeriod2 = 1print("Test case: 2")print("Principle amount:", principle2)print("Rate of interest:", rate2)print("Time period:", timePeriod2)print("Simple Interest:", calculateSimpleInterest(principle2, rate2, timePeriod2))principle3 = 5800rate3 = 4timePeriod3 = 6print("Test case: 3")print("Principle amount:", principle3)print("Rate of interest:", rate3)print("Time period:", timePeriod3)print("Simple Interest:", calculateSimpleInterest(principle3, rate3, timePeriod3))``

Go out:

``Test case: 1Principle amount: 1000Rate of interest: 7Time period: 2Simple Interest: 140.0Test case: 2Principle amount: 5000Rate of interest: 5Time period: 1Simple Interest: 250.0Test case: 3Principle amount: 5800Rate of interest: 4Time period: 6Simple Interest: 1392.0``

Related: How To Complete The FizzBuzz Challenge In Different Programming Languages

### The JavaScript program to calculate simple interest

Below is the JavaScript program to calculate simple interest:

``// JavaScript program to calculate simple interest// for given principle amount, time, and rate of interest.// Function to calculate simple interestfunction calculateSimpleInterest(principle, rate, timePeriod) {return (principle * rate * timePeriod) / 100;}var principle1 = 1000;var rate1 = 7;var timePeriod1 = 2;document.write("Test case: 1" + "");document.write("Principle amount: " + principle1 + "");document.write("Rate of interest: " + rate1 + "");document.write("Time period: " + timePeriod1 + "");document.write("Simple Interest: " + calculateSimpleInterest(principle1, rate1, timePeriod1) + "");var principle2 = 5000;var rate2 = 5;var timePeriod2 = 1;document.write("Test case: 2" + "");document.write("Principle amount: " + principle2 + "");document.write("Rate of interest: " + rate2 + "");document.write("Time period: " + timePeriod2 + "");document.write("Simple Interest: " + calculateSimpleInterest(principle2, rate2, timePeriod2) + "");var principle3 = 5800;var rate3 = 4;var timePeriod3 = 6;document.write("Test case: 3" + "");document.write("Principle amount: " + principle3 + "");document.write("Rate of interest: " + rate3 + "");document.write("Time period: " + timePeriod3 + "");document.write("Simple Interest: " + calculateSimpleInterest(principle3, rate3, timePeriod3) + "");``

Go out:

``Test case: 1Principle amount: 1000Rate of interest: 7Time period: 2Simple Interest: 140Test case: 2Principle amount: 5000Rate of interest: 5Time period: 1Simple Interest: 250Test case: 3Principle amount: 5800Rate of interest: 4Time period: 6Simple Interest: 1392``

How to calculate compound interest

Compound interest is the addition of interest to the principal amount. In other words, it’s interest over interest. You can calculate compound interest using the following formula:

`` Amount= P(1 + R/100)TCompound Interest = Amount â€“ PWhere,P = Principle AmountR = RateT = Time``

### The problem statement

we give you principal amount, interest rate, and time. You need to calculate and print the compound interest for the given values. Example: Either principle = 1000, rate = 7 and timePeriod = 2. Amount = P (1 + R / 100) T = 1144.9 Compound interest = Amount – Principal amount = 1144.9 – 1000 = 144.9 Thus, the output is 144.9.

### The C ++ program to calculate compound interest

Below is the C ++ program to calculate compound interest:

``// C++ program to calculate compound interest// for given principle amount, time, and rate of interest.#include using namespace std;// Function to calculate compound interestfloat calculateCompoundInterest(float principle, float rate, float timePeriod){double amount = principle * (pow((1 + rate / 100), timePeriod));return amount - principle;}int main(){float principle1 = 1000;float rate1 = 7;float timePeriod1 = 2;cout << "Test case: 1" << endl;cout << "Principle amount: " << principle1 << endl;cout << "Rate of interest: " << rate1 << endl;cout << "Time period: " << timePeriod1 << endl;cout << "Compound Interest: " << calculateCompoundInterest(principle1, rate1, timePeriod1) << endl;float principle2 = 5000;float rate2 = 5;float timePeriod2 = 1;cout << "Test case: 2" << endl;cout << "Principle amount: " << principle2 << endl;cout << "Rate of interest: " << rate2 << endl;cout << "Time period: " << timePeriod2 << endl;cout << "Compound Interest: " << calculateCompoundInterest(principle2, rate2, timePeriod2) << endl;float principle3 = 5800;float rate3 = 4;float timePeriod3 = 6;cout << "Test case: 3" << endl;cout << "Principle amount: " << principle3 << endl;cout << "Rate of interest: " << rate3 << endl;cout << "Time period: " << timePeriod3 << endl;cout << "Compound Interest: " << calculateCompoundInterest(principle3, rate3, timePeriod3) << endl;return 0;}``

Go out:

``Test case: 1Principle amount: 1000Rate of interest: 7Time period: 2Compound Interest: 144.9Test case: 2Principle amount: 5000Rate of interest: 5Time period: 1Compound Interest: 250Test case: 3Principle amount: 5800Rate of interest: 4Time period: 6Compound Interest: 1538.85``

Related: How to Invert an Array in C ++, Python, and JavaScript

### The Python program to calculate compound interest

Below is the Python program to calculate compound interest:

``# Python program to calculate compound interest# for given principle amount, time, and rate of interest.# Function to calculate compound interestdef calculateCompoundInterest(principle, rate, timePeriod):amount = principle * (pow((1 + rate / 100), timePeriod))return amount - principleprinciple1 = 1000rate1 = 7timePeriod1 = 2print("Test case: 1")print("Principle amount:", principle1)print("Rate of interest:", rate1)print("Time period:", timePeriod1)print("Compound Interest:", calculateCompoundInterest(principle1, rate1, timePeriod1))principle2 = 5000rate2 = 5timePeriod2 = 1print("Test case: 2")print("Principle amount:", principle2)print("Rate of interest:", rate2)print("Time period:", timePeriod2)print("Compound Interest:", calculateCompoundInterest(principle2, rate2, timePeriod2))principle3 = 5800rate3 = 4timePeriod3 = 6print("Test case: 3")print("Principle amount:", principle3)print("Rate of interest:", rate3)print("Time period:", timePeriod3)print("Compound Interest:", calculateCompoundInterest(principle3, rate3, timePeriod3))``

Go out:

``Test case: 1Principle amount: 1000Rate of interest: 7Time period: 2Compound Interest: 144.9000000000001Test case: 2Principle amount: 5000Rate of interest: 5Time period: 1Compound Interest: 250.0Test case: 3Principle amount: 5800Rate of interest: 4Time period: 6Compound Interest: 1538.8503072768026``

Related: How To Find The Sum Of All The Elements In An Array

### The JavaScript program to calculate compound interest

Below is the JavaScript program to calculate compound interest:

``// JavaScript program to calculate compound interest// for given principle amount, time, and rate of interest.// Function to calculate compound interestfunction calculateCompoundInterest(principle, rate, timePeriod) {var amount = principle * (Math.pow((1 + rate / 100), timePeriod));return amount - principle;}var principle1 = 1000;var rate1 = 7;var timePeriod1 = 2;document.write("Test case: 1" + "");document.write("Principle amount: " + principle1 + "");document.write("Rate of interest: " + rate1 + "");document.write("Time period: " + timePeriod1 + "");document.write("Compound Interest: " + calculateCompoundInterest(principle1, rate1, timePeriod1) + "");var principle2 = 5000;var rate2 = 5;var timePeriod2 = 1;document.write("Test case: 2" + "");document.write("Principle amount: " + principle2 + "");document.write("Rate of interest: " + rate2 + "");document.write("Time period: " + timePeriod2 + "");document.write("Compound Interest: " + calculateCompoundInterest(principle2, rate2, timePeriod2) + "");var principle3 = 5800;var rate3 = 4;var timePeriod3 = 6;document.write("Test case: 3" + "");document.write("Principle amount: " + principle3 + "");document.write("Rate of interest: " + rate3 + "");document.write("Time period: " + timePeriod3 + "");document.write("Compound Interest: " + calculateCompoundInterest(principle3, rate3, timePeriod3) + "");``

Go out:

``Test case: 1Principle amount: 1000Rate of interest: 7Time period: 2Compound Interest: 144.9000000000001Test case: 2Principle amount: 5000Rate of interest: 5Time period: 1Compound Interest: 250Test case: 3Principle amount: 5800Rate of interest: 4Time period: 6Compound Interest: 1538.8503072768008``

These days, the impact of coding is increasing exponentially. And at the same time, the demand for skilled coders is also increasing exponentially. There is a misconception among people that they can only learn to code after paying high fees. But this is not true. You can learn to code absolutely free from platforms like freeCodeCamp, Khan Academy, YouTube, etc. So even if you don’t have a big budget, you don’t have to worry about missing something.