2 min to read
HackerRank 30 Days of Code Solution Day 2: Operators
In this tutorial, we will guide you through the process of solving the Day 2 Operators programming problem from HackerRank’s “30 Days of Code” challenge.
We will provide comprehensive code solutions in C++, Python, and JavaScript programming languages. With our help, you will gain a better understanding of the problem and learn how to approach and solve similar coding challenges.
Disclaimer: We encourage you to solve this challenge yourself before reading our tutorial. We have provided a detailed explanation of the problem and our solutions to help you check your work.
Hackerrank 30 days of code - Day 2: Operators
It’s part of hackerrank’s 30 days of code. A series of 30-day programming challenges. Where you can learn new programming languages by solving the coding challenges.
Problem Statement
In this problem, we have given the Base Meal Cost
, Tip Percent
, and Tax Percent
. We have to calculate the total cost of the meal.
The formula to calculate the total cost of the meal is:
Total cost of the meal = meal cost + tip cost + tax cost
Suppose the base meal cost is 100 rupees, the tip percent is 10% and the tax percent is 5% then the total cost of the meal will be 100 + 10 + 5 = 115 rupees. The output will be 115. We have to round the result to the nearest integer.
Input | Data Type |
---|---|
meal_cost | int |
tip_percent | int |
tax_percent | int |
Operators Hackerrank solution in c++(cpp)
Operators Hackerrank solution in Python
Operators Hackerrank solution in JavaScript
Explanation
In the above code, we have taken the input from the user and stored it in the variable. Then we calculated the total cost of the meal and rounded it to the nearest integer. Then we have printed the result.
The Tip cost is calculated by multiplying the meal cost with the tip percent and dividing it by 100. The Tax cost is calculated by multiplying the meal cost with the tax percent and dividing it by 100. The total cost of the meal is calculated by adding the meal cost, tip cost, and tax cost.
To round the result to the nearest integer we have used the round()
function. The round()
function takes two arguments. The first argument is the number to be rounded and the second argument is the number of digits after the decimal point. If the second argument is not given then the number is rounded to the nearest integer.