Day 2: Operators | 30 Days of Code | Hackerrank Solution

Day 2 Operators Solution is the second day problem of Hackerrank 30 Days of Code. In this challenge we have meal price (base cost of a meal), tip perc

Day 2: Operators | 30 Days of Code | Hackerrank Solution

Day 2 Operators Solution is the second day problem of Hackerrank 30 Days of Code. In this challenge we have meal price (base cost of a meal), tip percent (the percentage of tip to be added to meal price) and tax percent (the percent of tax to be added to meal price) for a meal.

We have to find the meal's total price. Such kind of programs is used in the billing software of cafeteria and restaurant to calculate the bills.

After going through this tutorial you will not only able to solve this problem but you can also make your own billing software which can be used in small cafeterias or food parlors.

So, Without further ado let's jump into the problem.

Day 2 Operators 30 Days of Code HackerRank Solution Sloth Coders

Objective

In this challenge, you'll work with arithmetic operators. 

Arithmetic Operators

The binary Operators used for arithmetic are as follows:

  • + : Addition
  • - : Subtraction
  • * - Multiplication
  • / - Division
  • % - Remainder

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 value for your calculations, or you may end up with an incorrectly rounded result!

Input Format for operators HackerRank Solution

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 tip).

Output Format for operators HackerRank Solution

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

Sample Input

12.00
20
8

Sample Output 

15

Explanation for Operators HackerRank Solution

Given:

mealCost = 12, tipPercent = 20, taxPercent = 8

Calculations:

Step:1 Tip = Mealcost * tip percent / 100

tip = 12 * ( 20 / 100 ) = 2.4

Step:2 Tax = Mealcost * tax percent / 100

tax = 12 * ( 8 / 100 ) = 0.96

Step:3 Total = Mealcost + Tip + Tax

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

round( totalCost ) = 15

We round totalCost to the nearest dollar (integer) and then prints out result, 15.

So, final bill is 15 dollars paid by customer in cafeteria.

Solution - Day 2: Operators 30 Days of Code in Python 

Python 3 Solution 

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the solve function below.
def solve(meal_cost, tip_percent, tax_percent):
    tip = tip_percent * meal_cost / 100 
    tax = tax_percent * meal_cost / 100
    total = round( meal_cost + tip + tax )
    print(total)

if __name__ == '__main__':
    meal_cost = float(input())

    tip_percent = int(input())

    tax_percent = int(input())

    solve(meal_cost, tip_percent, tax_percent)
Day 2 Operators 30 Days of Code Hacker Rank Solution in C++
Day 2: Operators in C++ - Hacker Rank Solution

Day 2 Operators in C++ 30 Days of Code Hacker Rank Solution
Day 2: Operators in C++ - Result


Conclusion

This was the solution of our Day 2 operators problem. If you have any doubt regarding the problem (Day 2: Operators), feel free to contact in the Comment section.

Don't forget to Share this post with your friends and relatives and Subscribe our Blog to get latest updates about coding.

Disclaimer: The above problem is generated by Hacker Rank but the solution is provided by Sloth Coders.

Happy Coding!!

A Sloth Who loves to Code.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment