Simple interest calculator

Report a typo

Suppose you want to create a Go program that performs simple interest calculations. Since you've learned about functional decomposition, you want to apply it to this program and divide it into three simple functions:

  • getData() — Should ask the user to input the amount of money, the interestRate and the numberOfYears, and then return those three variables.

  • calculateSimpleInterest() — Should perform interest calculation and return the final interest amount and the total sum.

  • printResult() — Should print out the calculated simpleInterest first and the totalAmount afterward in two separate lines in the following format, with two decimal places:

The interest is 400.00
The total amount is 1400.00

Here is the formula for calculating the interest: interest=amount×interest  rate×time100 interest = \frac {amount \times interest\;rate \times time}{100}

In the Sample Input below, the first number is the starting amount of money, followed by the interestRate in %, and last is the numberOfYears.

To make this coding challenge simpler and avoid type casting, every variable must be of the float64 type.

Sample Input 1:

2000.00
5
4

Sample Output 1:

The simple interest is 400.00
The total amount is 2400.00

Sample Input 2:

8000.00
5.5
2.5

Sample Output 2:

The simple interest is 1100.00
The total amount is 9100.00
Write a program in Go
package main

import "fmt"

// getData() asks the user to input the `amount` of money, then the `interestRate`
// and last the `numberOfYears` and then returns those three variables
func getData() (float64, float64, float64) {
var amount, interestRate, numberOfYears float64

fmt.Scanln(?) // Read the `amount` of money first
fmt.Scanln(?) // Read the `interestRate` second
fmt.Scanln(?) // Read the `numberOfYears` last

return amount, interestRate, numberOfYears
}

// calculateSimpleInterest() takes as arguments the `amount`, `interestRate` and `numberOfYears`
// and uses the formula from the problem statement to calculate the `simpleInterest`
// nolint: gomnd // <-- DO NOT delete!
func calculateSimpleInterest(?, ?, ? float64) float64 {
simpleInterest := ?
return simpleInterest
}

// printResult() prints the calculated `simpleInterest` and `totalAmount`
// With two decimal places using fmt.Printf() with the `%.2f` format specifier
func printResult(?, ? float64) {
fmt.Printf("The simple interest is %.2f\n", ?)
fmt.Printf("?", totalAmount)
}

// DO NOT delete or modify the contents of the main() function!
func main() {
amount, interestRate, numberOfYears := getData()
simpleInterest := calculateSimpleInterest(amount, interestRate, numberOfYears)
printResult(simpleInterest, amount+simpleInterest)
___

Create a free account to access the full topic