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.
Operators hackerrank solution in c++(cpp)
#include <bits/stdc++.h>
using namespace std;
// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent) {
int total_cost;
total_cost = meal_cost + meal_cost * tip_percent/100 + meal_cost * tax_percent/100;
cout << total_cost <<endl;
}
int main()
{
double meal_cost;
cin >> meal_cost;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int tip_percent;
cin >> tip_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int tax_percent;
cin >> tax_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
solve(meal_cost, tip_percent, tax_percent);
return 0;
}
Operators hackerrank solution in Python
import math
import os
import random
import re
import sys
# Complete the solve function below.
def solve(meal_cost, tip_percent, tax_percent):
total_cost = meal_cost + meal_cost * tip_percent/100 + meal_cost * tax_percent/100
print(round(total_cost))
if __name__ == '__main__':
meal_cost = float(input())
tip_percent = int(input())
tax_percent = int(input())
solve(meal_cost, tip_percent, tax_percent)
Operators hackerrank solution in JavaScript
// Complete the solve function below.
function solve(meal_cost, tip_percent, tax_percent) {
let total_cost;
total_cost = meal_cost + meal_cost * tip_percent/100 + meal_cost * tax_percent/100;
console.log(Math.round(total_cost));
}
function main() {
const meal_cost = parseFloat(readLine());
const tip_percent = parseInt(readLine(), 10);
const tax_percent = parseInt(readLine(), 10);
solve(meal_cost, tip_percent, tax_percent);
}
Problem Statement Link :
https://www.hackerrank.com/challenges/30-operators/problem
Check out 30 Days of Code| CodePerfectplus for All solutions from this series.
Top comments (2)
thats a nice code but it failed in test case 3 because instead of converting value to int we should use round function it will give you nearby value of double value, using this will be better :)
include
using namespace std;
// Complete the solve function below.
void solve(double meal_cost, double tip_percent, double tax_percent) {
}
int main()
{
double meal_cost;
cin >> meal_cost;
cin.ignore(numeric_limits::max(), '\n');
}
in python .
i put return instead of print but it doesn't worked.
can you explain why?