30 Days of Code - Day 2: Operators

Coding challenges are a great resource for learning coding techniques and improve analytical thinking, this is a collection of challenges from different platforms.

Objective

In this challenge, you’ll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal’s total cost.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!

Input Format

There are 3 lines of numeric input:

The first line has a double, mealCost (the cost of the meal before tax and tip).

The second line has an integer, tipPercent (the percentage of mealCost being added as tip).

The third line has an integer, taxPercent (the percentage of mealCost being added as tax).

Output Format

Print The total meal cost is totalCost dollars., where totalCost is the rounded integer result of the entire bill (mealCost with added tax and tip).

Sample 00

input00.txt
12.00
20
8
output00.txt
15

Explanation

Given:

mealCost = 12, tipPercent = 20, taxPercent = 8

Calculations:

tip = 12 * (20 / 100) = 2.4

tax = 12 * (8 / 100) = 0.96

totalCost = mealCost + tip + tax = 12 + 2.4 + 0.96 = 15.36

roud(totalCost) = 15

We round totalCost to the nearest dollar (integer) and then print our result:

The total meal cost is 15 dollars.

Sample 01

input01.txt
15.50
15
10
output01.txt
19

Solution

main.go
package main

import (
  "fmt"
)

func main() {
  var (
    mealCost float64

    tipPercent, taxPercent int

    total float64
  )

  fmt.Scan(&mealCost)
  total += mealCost

  fmt.Scan(&tipPercent)
  total += float64(tipPercent) / 100 * mealCost

  fmt.Scan(&taxPercent)
  total += float64(taxPercent) / 100 * mealCost

  fmt.Println(int64(total + 0.5))
}