# Day 2: Operators | 30 Days Of Code | HackerRank Solution

Hello coders, today we are going to solve Day 2: Operators HackerRank Solution in C++, Java and Python.

## Objective

In this challenge, you will work with arithmetic operators.

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. Round the result to the nearest integer.

Example

mealcost = 100
tippercent = 15
taxpercent = 8

A tip of 15% * 100 = 15, and the taxes are 8% * 100 = 8. Print the value 123 and return from the function.

Function Description
Complete the solve function in the editor below.

solve has the following parameters:

• int meal_cost: the cost of food before tip and tax
• int tip_percent: the tip percentage
• int tax_percent: the tax percentage

Returns The function returns nothing. Print the calculated value, rounded to the nearest integer.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result.

## Input Format

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

Sample Input

``````12.00
20
8``````

Sample Output

``15``

Explanation

Given:
mealcost = 12tip_percent = 20tax_percent = 8

Calculations:
tip = 12 and 12/100 * 20 = 2.4
tax = 8 and 8/100 * 20 = 0.96

total_cost = meal_cost + tip + tax = 12 + 2.4 + 0.96 = 15.36
round(total_cost) = 15
We round total_cost to the nearest integer and print the result, 15.

## Solution – Day 2: Operators Solution

### C

```#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent)
{
double tip=meal_cost*tip_percent/100;
double tax=meal_cost*tax_percent/100;
int totalCost=(int)round(meal_cost+tip+tax);
printf("%d",totalCost);
}

int main()
{
char* meal_cost_endptr;
double meal_cost = strtod(meal_cost_str, &meal_cost_endptr);

if (meal_cost_endptr == meal_cost_str || *meal_cost_endptr != '\0')
{
exit(EXIT_FAILURE);
}

char* tip_percent_endptr;
int tip_percent = strtol(tip_percent_str, &tip_percent_endptr, 10);

if (tip_percent_endptr == tip_percent_str || *tip_percent_endptr != '\0')
{
exit(EXIT_FAILURE);
}

char* tax_percent_endptr;
int tax_percent = strtol(tax_percent_str, &tax_percent_endptr, 10);

if (tax_percent_endptr == tax_percent_str || *tax_percent_endptr != '\0')
{
exit(EXIT_FAILURE);
}

solve(meal_cost, tip_percent, tax_percent);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) { break; }

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n')
{
break;
}

size_t new_length = alloc_length << 1;
data = realloc(data, new_length);

if (!data) { break; }

alloc_length = new_length;
}

if (data[data_length - 1] == '\n')
{
data[data_length - 1] = '\0';
}

data = realloc(data, data_length);

return data;
}```

### C++

```#include <bits/stdc++.h>

using namespace std;

// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent)
{
double tip=meal_cost*tip_percent/100;
double tax=meal_cost*tax_percent/100;
int totalCost=(int)round(meal_cost+tip+tax);
cout<<totalCost;
}

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;
}
```

### Java

```import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// Complete the solve function below.
static void solve(double meal_cost, int tip_percent, int tax_percent)
{
double tip=meal_cost*tip_percent/100;
double tax=meal_cost*tax_percent/100;
int totalCost=(int)Math.round(meal_cost+tip+tax);
System.out.print(totalCost);
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
double meal_cost = scanner.nextDouble();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int tip_percent = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int tax_percent = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

solve(meal_cost, tip_percent, tax_percent);

scanner.close();
}
}```

### 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)
```

### JavaScript

```'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.split('\n');

main();
});

return inputString[currentLine++];
}

/*
* Complete the 'solve' function below.
*
*
* The function accepts following parameters:
*  1. DOUBLE meal_cost
*  2. INTEGER tip_percent
*  3. INTEGER tax_percent
*/

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

solve(meal_cost, tip_percent, tax_percent);
}
```

Disclaimer: The above Problem (Day 2: Operators) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.