Kotlin Program to Calculate Compound Interest


In this article, we will understand how to calculate the Compound interest. The percentage interest charged on principal and accrued interest is Compound Interest. Rates are higher compared to Simple Interest. Compound Interest is calculated using the formula

principle * (Math.pow((1 + rate / 100), time)) – principle

Below is a demonstration of the same

Suppose our input is

Principle number: 100000
Interest rate: 5
Time period in years: 3

The desired output would be −

The Compound Interest is: 15762.50000000001

Algorithm

  • Step 1 − START

  • Step 2 − Declare four integer values principalAmount, interestRate, timePeriod, compoundInterest

  • Step 3 − Define the values for principalAmount, interestRate, timePeriod, compoundInterest.

  • Step 4 − Perform “principle * (Math.pow((1 + rate / 100), time)) – principle” to calculate the compound interest and store it in a compoundInterest variable

  • Step 5 − Display compoundInterest

  • Step 6 − STOP

Example 1

In this example, we will calculate Compound Interest in Kotlin using the above given formulae. First, declare and set the variable for the Principal amount −

val principalAmount = 10000

Then, set the variables for rate of interest and time period −

val interestRate = 5
val timePeriod = 3

Now, use the above formulae to calculate the Compound Interest

val compoundInterest = principalAmount.toDouble() * Math.pow((1 +
interestRate.toDouble()/100.00),timePeriod.toDouble())- principalAmount

Let us see the example to compute the Compound Interest in Kotlin −

fun main() { val principalAmount = 10000 println("Principal amount is defined as: $principalAmount") val interestRate = 5 println("The rate of interest is defined as: $interestRate %") val timePeriod = 3 println("The time period is defined as: $timePeriod years") val compoundInterest = principalAmount.toDouble() * Math.pow((1 + interestRate.toDouble()/100.00),timePeriod.toDouble())- principalAmount println("
Compound Interest is: $compoundInterest"
) }

Output

Principal amount is defined as: 10000
The rate of interest is defined as: 5 %
The time period is defined as: 3 years
Compound Interest is: 1576.250000000002

Example 2

In this example, we will calculate Compound Interest in Kotlin

fun main() { val principalAmount = 10000 println("Principal amount is defined as: $principalAmount") val interestRate = 5 println("The rate of interest is defined as: $interestRate %") val timePeriod = 3 println("The time period is defined as: $timePeriod years") getCompoundInterest(principalAmount, interestRate, timePeriod) } fun getCompoundInterest(principalAmount: Int, interestRate: Int, timePeriod: Int) { val compoundInterest = principalAmount.toDouble() * Math.pow((1 + interestRate.toDouble()/100.00),timePeriod.toDouble()) - principalAmount println("
Compound Interest is: $compoundInterest"
) }

Output

Principal amount is defined as: 10000
The rate of interest is defined as: 5 %
The time period is defined as: 3 years
Compound Interest is: 1576.250000000002

Updated on: 13-Oct-2022

296 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements