# Kotlin Program to calculate Simple Interest and Compound Interest

Kotlin Server Side ProgrammingProgramming

#### Kotlin Online Training

68 Lectures 4.5 hours

#### Kotlin Masterclass Programming: Android Coding Bible

71 Lectures 5.5 hours

#### Android Dependency Injection using Dagger with Kotlin

18 Lectures 1.5 hours

In this article, we will input the Principal Amount, Rate and Time (Years) from the user to find the Simple Interest and Compound Interest −

• Simple Interest − The percentage interest on total principal amount. Returns are less compared to Compound Interest.

• Compound Interest − The percentage interest charged on principal and accrued interest. Rates are higher compared to Simple Interest.

Below is a demonstration of the same −

Suppose our input is −

Principal = 25000.0
Annual Rate of Interest = 10.0
Time (years) = 4.0


The desired output would be −

Simple Interest: 10000.0
Compound Interest: 11602.500000000007


## Algorithm

• Step 1 − START

• Step 2 − Declare 5 integers principalAmount, interestRate, timePeriod, simpleInterest and compoundInterest

• Step 3 − Perform (p * r * t)/100

• Step 4 − Perform p * Math.pow(1.0+r/100.0,t) - p;

• Step 5 − Store the output of Step 4 in simpleInterest

• Step 6 − Store the output of Step 5 in compoundInterest

• Step 7 − Display simpleInterest

• Step 8 − Display compoundInterest

• Step 9 − STOP

## Example 1

In this example, we will Calculate Simple Interest and Compound Interest using the widely used formulas. First, declare a variable for principal amount

val principalAmount = 10000


Now, declare and the rate of interest and the time period −

val interestRate = 5
println("The rate of interest is defined as: $interestRate %") val timePeriod = 2 println("The time period is defined as:$timePeriod years")


Use the formulas to calculate the Simple and Compound Interest

val simpleInterest = (principalAmount*interestRate*timePeriod)/100
println(" Simple Interest is: $simpleInterest") val compoundInterest = principalAmount.toDouble() * Math.pow((1 + interestRate.toDouble()/100.00),timePeriod.toDouble()) - principalAmount println(" Compound Interest is :$compoundInterest")


Let us now see the complete example to calculate the Simple and Compound Interest −

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 = 2
println("The time period is defined as: $timePeriod years") val simpleInterest = (principalAmount*interestRate*timePeriod)/100 println(" Simple Interest is:$simpleInterest")

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: 2 years Simple Interest is: 1000 Compound Interest is: 1025.0  ## Example 2 In this example, we will calculate simple interest and compound interest − 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")
getInterest(principalAmount, interestRate, timePeriod)
}

fun getInterest(principalAmount: Int, interestRate: Int, timePeriod: Int) {
val simpleInterest = (principalAmount*interestRate*timePeriod)/100
println(" Simple Interest is: $simpleInterest") 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

Simple Interest is: 1500
Compound Interest is: 1576.2500000000018

Updated on 13-Oct-2022 13:33:15