DEV Community

Cover image for Day 2: Operators - 30 Days of Code HackerRank
Aayushi Sharma
Aayushi Sharma

Posted on • Updated on • Originally published at codeperfectplus.com

Day 2: Operators - 30 Days of Code HackerRank

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.

Day 2: Operators - 30 Days of Code HackerRank

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;
}
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
saikartik101 profile image
Saikartik101 • Edited

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) {

double total_cost;
total_cost =  meal_cost + meal_cost * tip_percent/100 + meal_cost * tax_percent/100;
cout.precision(10);
cout <<round(total_cost) <<endl;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
double meal_cost;
cin >> meal_cost;
cin.ignore(numeric_limits::max(), '\n');

double tip_percent;
cin >> tip_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

double tax_percent;
cin >> tax_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

solve(meal_cost, tip_percent, tax_percent);

return 0;
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
vignxs profile image
vignxs

in python .
i put return instead of print but it doesn't worked.
can you explain why?