HackerRank Day 2 Solution in Python: Operators

Today we will see the HackerRank Day 2 Solution in Python. The problem is named Operators which is part of 30 Days of code on HackerRank. Let’s get started!

Day 2: Operators Problem statement

We are given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as a tip), and tax percent (the percentage of the meal price being added as tax) for a meal. Our task is to find and print the meal’s total cost. Also, we have to round the result to the nearest integer.

Sample Input

12.00
20
8

Sample Output

15

Explanation: A tip of 20% means 12*20/100 = 2.4, and the taxes are 8% means 8*2/100 = 0.96. So the total of three is 15. Hence, print the value 15 in the function.

You can solve the problem here.

HackerRank Day 2 Solution in Python

#!/bin/python3

import math
import os
import random
import re
import sys

#Function to calculate the total amount
def solve(meal_cost, tip_percent, tax_percent):
    
    #calculate tip amount
    tip=meal_cost*(tip_percent/100)
    
    #Calculate tax amount
    tax=meal_cost*(tax_percent/100)
    
    #Add meal cost, tip amount and tax amount to get total amount
    total=round(meal_cost+tip+tax) 
    
    #Print the total amount
    print(total)

#Input of meal cost, tip percent and tax percent 
meal_cost = float(input())
tip_percent = int(input())
tax_percent = int(input())

#Function call to calculate total amount
solve(meal_cost, tip_percent, tax_percent)

Code Explanation

  • First, we create a function solve to calculate the total amount of the bill
  • In the method, calculate the tip amount by the formula, tip=meal_cost(tip_percent/100) and calculate the tax amount by tax=meal_cost(tax_percent/100)
  • Then add meal cost, tip amount, and tax amount to get the total amount of the bill
  • Finally, print the total amount of the bill

Also Read:

Share:

Author: Ayush Purawr