Program to calculate compound interest in Kotlin

In this article, We will learn program to calculate compound interest in Kotlin. In simple terms, Compound interest is the sum of principal amount and interest of interest.

Compound Interest Formula Kotlin
Compound Interest Formula

Source Code:
fun main(args: Array<String>)
        print("Enter principle amount : ")
        val principle = readLine()!!
        print("Enter interest rate : ")
        val rate = readLine()!!
        print("Enter number of years : ")
        val time = readLine()!!

        val ci = principle.toDouble() * Math.pow((1 + rate.toDouble()/100.00),time.toDouble())
        println("Compound Interest is : $ci")
    catch (exception : NumberFormatException)
Enter principle amount : 2000
Enter interest rate : 2
Enter number of years : 3
Compound Interest is : 2122.416
Calculate Compound Interest in Kotlin - Output
In this program, we will take input from the user to enter to get principal amount, rate and years value. To do so we have used readLine() function.

compound interest formula is A = P(1 + r/n)^nt. Where "P" is the principal amount, "r" is an annual nominal interest rate and "n" is the number of years. Same is printed using print() function.

Post a Comment