Calculate simple interest in Kotlin:
In this kotlin programming tutorial, we will learn how to calculate simple interest by using the user provided values. The user will enter the values and the program will calculate the simple interest.
Algorithm :
Our program will use the below algorithm :
-
Create three variables to store the_ principal amount, rate of interest and number of years_ to calculate the simple interest.
-
Calculate the simple interest and store it in a different variable.
-
Print out the simple interest.
-
Exit.
Kotlin program :
import java.util.Scanner
fun main(args: Array<string>) {
//1
val scanner = Scanner(System.`in`)
//2
print("Enter principal amount : ")
var p:Int = scanner.nextInt()
//3
print("Enter rate of interest : ")
var r:Int = scanner.nextInt()
//4
print("Enter number of years : ")
var n:Int = scanner.nextInt()
//5
var SI:Int = (p*n*r)/100
//6
println("Simple interest : "+SI)
}
Explanation :
The commented numbers in the above program denote the step numbers below :
-
Create one Scanner object to read the user input. This is the same Scanner class object that we use in Java.
-
Ask the user to enter the principal amount. Read it using the scanner object and save it in variable p.
-
Ask the user to enter the rate of interest. Read it and store it in variable_ r_.
-
Ask the user to enter the number of years. Read it and store it in n
-
Calculate the simple interest and store it in the variable SI.
-
Print out the result to the user.
Sample Output :
Enter principal amount : 100
Enter rate of interest : 10
Enter number of years : 10
Simple interest : 100
Enter principal amount : 1200
Enter rate of interest : 12
Enter number of years : 4
Simple interest : 576
You can download this program from here.