The Wayback Machine - https://web.archive.org/web/20241001054051/https://www.geeksforgeeks.org/python-program-for-compound-interest/
Open In App

Python Program for Compound Interest

Last Updated : 03 Jul, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Let us discuss the formula for compound interest. The formula to calculate compound interest annually is given by: 

A = P(1 + R/100) t 

Compound Interest = A – P 

Where, 

  • A is amount 
  • P is the principal amount 
  • R is the rate and 
  • T is the time span

Find Compound Interest with Python

Python3




# Python3 program to find compound
# interest for given values.
 
 
def compound_interest(principal, rate, time):
 
    # Calculates compound interest
    Amount = principal * (pow((1 + rate / 100), time))
    CI = Amount - principal
    print("Compound interest is", CI)
 
 
# Driver Code
compound_interest(10000, 10.25, 5)


Output

Compound interest is 6288.946267774416

Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant.

Compound Interest with Input taking from user

In this method we are going to calculate the compound interest by taking input from the user by using above formula.

Python3




# Python3 program to find compound
# interest for input taking from user.
 
 
def compound_interest(principal, rate, time):
 
    # Calculates compound interest
    Amount = principal * (pow((1 + rate / 100), time))
    CI = Amount - principal
    print("Compound interest is", CI)
 
 
# Driver Code
#Taking input from user.
principal = int(input("Enter the principal amount: "))
rate = int(input("Enter rate of interest: "))
time = int(input("Enter time in years: " ))
#Function Call
compound_interest(principal,rate,time)
 
#This code is contributed by Vinay Pinjala.


Output:

Input:
Enter the principal amount: 3000
Enter rate of interest: 5
Enter time in years: 3
Output:
Compound interest is 472.875

Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant.

Finding compound interest of given values without using pow() function.

Python3




# Python code
# To find compound interest
 
# inputs
p= 1200   # principal amount
t= 2      # time
r= 5.4    # rate
# calculates the compound interest
a=p*(1+(r/100))**t  # formula for calculating amount
ci=a-p  # compound interest = amount - principal amount
# printing compound interest value
print(ci)


Output

133.0992000000001

Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant

Please refer complete article on Program to find compound interest for more details!

Compound Interest using for loop

Python3




def compound_interest(principal, rate, time):
    Amount = principal
    for i in range(time):
        Amount = Amount * (1 + rate/100)
    CI = Amount - principal
    print("Compound interest is", CI)
# Driver Code
compound_interest(1200, 5.4, 2)
#This code is contributed by Jyothi pinjala


Output

Compound interest is 133.0992000000001

Time complexity: O(1)
Auxiliary Space: O(1) 



Previous Article
Next Article

Similar Reads

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) * 100) / N1 where N1 is the compound interest of so
3 min read
Program to find compound interest
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 in
4 min read
JavaScript program to find compound interest
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 in
2 min read
Python - Compound Interest GUI Calculator using Tkinter
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the Python GUI Libraries, Tkinter is the most commonly used method. In this article, we will learn how to create a Compound Interest GUI Calculator application using Tkinter. Let’s create a GUI-based Compound Interest Calculator application. Importing the mod
6 min read
Compound Interest - Aptitude Questions and Answers
Compound interest is a type of interest that is calculated on the initial principal and all of its subsequent accumulated interest. It is different from the simple interest which only calculates the interest earned on the initial principal for a specific period of time. Compound interest has more potential to generate greater returns than simple in
7 min read
Python Program for Simple Interest
Simple interest formula is given by: Simple Interest = (P x T x R)/100 Where, P is the principal amount T is the time and R is the rate Examples: Input : P = 10000 R = 5 T = 5Output :2500.0We need to find simple interest on Rs. 10,000 at the rate of 5% for 5 units of time.Python Program for simple interest[GFGTABS] Python # Python3 program to find
2 min read
Program to find simple interest
What is 'Simple Interest'? Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula: Simple interest formula is given by: Simple Interest = (P x T x R)/100 Where, P
4 min read
JavaScript Program to find simple interest
What is ‘Simple Interest’?Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula:Simple interest formula is given by: Simple Interest = (P x T x R)/100 Where, P is
2 min read
Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
You have been given a series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!, find out the sum of the series till nth term. Examples: Input :n = 5 Output : 2.70833 Input :n = 7 Output : 2.71806 C/C++ Code # Python code to find smallest K-digit # number divisible by X def sumOfSeries(num): # Computing MAX res = 0 fact = 1 for i in range(1, num+1): fact *=
1 min read
Python Program for Program to Print Matrix in Z form
Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 C/C++ Code # Python program to print a # square matrix in Z form arr = [[4, 5, 6, 8
2 min read
Python Program for Program to calculate area of a Tetrahedron
A Tetrahedron is simply a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides or lateral faces, one on the bottom or the base and four vertices or corners. If the faces are all congruent equilateral triangles, then the tetrahedron is called regular. The volume of the tetrahedron can be found by using
2 min read
Python Program for Efficient program to print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After step 1, n must be
6 min read
Python Program for Program to cyclically rotate an array by one
Write a Python program for a given array, the task is to cyclically rotate the array clockwise by one time. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: arr[] = {5, 1, 2, 3, 4} Input: arr[] = {2, 3, 4, 5, 1}Output: {1, 2, 3, 4, 5} Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.Approach: Assign every element wi
4 min read
Python program to check if the list contains three consecutive common numbers in Python
Our task is to print the element which occurs 3 consecutive times in a Python list. Example : Input : [4, 5, 5, 5, 3, 8] Output : 5 Input : [1, 1, 1, 64, 23, 64, 22, 22, 22] Output : 1, 22 Approach : Create a list.Create a loop for range size – 2.Check if the element is equal to the next element.Again check if the next element is equal to the next
3 min read
Python Foreach: How to Program in Python
In many programming languages, the foreach loop is a convenient way to iterate over elements in a collection, such as an array or list. Python, however, does not have a dedicated foreach loop like some other languages (e.g., PHP or JavaScript). In this article, we will explore different ways to implement a foreach-like loop in Python, along with ex
3 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
Javascript Program for Program to cyclically rotate an array by one
Given an array, cyclically rotate the array clockwise by one. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: arr[] = {5, 1, 2, 3, 4}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Following are steps. 1) Store last element in a variable say x. 2) Shift all elements one position ahead. 3) Replace first element o
2 min read
Python Program to print digit pattern
The program must accept an integer N as the input. The program must print the desired pattern as shown in the example input/ output. Examples: Input : 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *'s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the fir
3 min read
Python program to convert time from 12 hour to 24 hour format
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. Note : Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on 24-hour clock. Examples : Input : 11:21:30 PMOutput : 23:21:30Input : 12:12:20 AMOutput : 00:12:20How to Convert AM/PM to 24 Hour Time Wh
3 min read
Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
The task is to write a Python Program to sort a list of tuples in increasing order by the last element in each tuple. Input: [(1, 3), (3, 2), (2, 1)] Output: [(2, 1), (3, 2), (1, 3)] Explanation: sort tuple based on the last digit of each tuple. Methods #1: Using sorted(). Sorted() method sorts a list and always returns a list with the elements in
7 min read
Python Program for Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. Python Program for Selection SortThe provided Python code demonstrates the Selection Sort algorithm. Selection Sort has a time
2 min read
Python Program for Insertion Sort
Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. Python Program for Insertion SortThe insertionSort function takes an array arr as input. It first calculates the length of the array (n). If the length is 0 or 1, the function returns immediately as an array with 0 or 1 element is considered already
2 min read
Python Program for Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then do some arithmetic to calculate the position of each object in the output sequence. Python Program for Counting SortThe provided Python code demonstrates Counting Sort, a non-comp
4 min read
Python Program for Radix Sort
In this article we are going to see Radix Sort with Python Radix Sort Algorithm PythonThe provided Python code implements Radix Sort, a non-comparative sorting algorithm that works by distributing elements into buckets based on their individual digits. The code defines a countingSort function, which performs counting sort based on the digit represe
3 min read
Python Program for Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. C/C++ Code # Python program for counting sort # The main function that sort the given string
2 min read
Python Program for ShellSort
In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h\'th element is sorted. Python Program for ShellSortThe provided Python code demonstrates Shell Sort, an optimization of the Insertion Sort algorithm. Shell Sort sorts element
2 min read
Python Program for Longest Common Subsequence
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, "abc", "abg", "bdf", "aeg", '"acefg", .. etc are subsequences of "abcdefg". So a string of length n has 2^n different possible s
3 min read
Python Program for Binary Search (Recursive and Iterative)
In a nutshell, this search algorithm takes advantage of a collection of elements that is already sorted by ignoring half of the elements after just one comparison. Compare x with the middle element.If x matches with the middle element, we return the mid index.Else if x is greater than the mid element, then x can only lie in the right (greater) half
4 min read
Python Program for Min Cost Path
Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only
2 min read
Article Tags :
Practice Tags :