In this HackerRank Day 2: Operators problem solution 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.

HackerRank Day 2: Operators problem solution

Problem solution in Python programming.

from decimal import Decimal
mealCost = float(input().strip())
tip = float(input().strip())
tax = float(input().strip())

tip = mealCost * tip/100.0
tax = mealCost * tax/100.0

totalCost = round(mealCost+tip+tax)

print ("The total meal cost is %d dollars." %totalCost)



Problem solution in Java programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Arithmetic {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double mealCost = scan.nextDouble(); // original meal price
        int tipPercent = scan.nextInt(); // tip percentage
        int taxPercent = scan.nextInt(); // tax percentage
        scan.close();
      
        // Write your calculation code here.
        double tip = mealCost * (tipPercent / 100.0);
        double tax = mealCost * (taxPercent / 100.0);
        double total = mealCost + tip + tax;
        
        // cast the result of the rounding operation to an int and save it as totalCost 
        int totalCost = (int) Math.round(total);
      
        // Print your result
        System.out.println("The total meal cost is "+totalCost+" dollars.");
    }
}




Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    float meal = 0;
    int tip = 0;
    int tax = 0;
    cin>>meal>>tip>>tax;
    
    meal = meal + (meal*tip/100) + (meal*tax/100);
    
    if( meal-(int)meal >= 0.50){
        meal = (int)(meal+1);
    }
    else{
        meal = (int)meal;
    }
    cout<<"The total meal cost is "<<meal<<" dollars.";
    
    return 0;
    
    
}



Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    double mealcost;
    int tip;
    int tax;
    
    scanf("%lf",&mealcost);
    scanf("%d",&tip);      
    scanf("%d",&tax);
    
    double totalcost = mealcost + mealcost*tip/100 + mealcost*tax/100;
    
    double roundcost = round(totalcost);
    
    printf("The total meal cost is %.0lf dollars.\n",roundcost);
    
    return 0;
}



Problem solution in Javascript programming.

function processData(input) {
    //Enter your code here
    var inputArray = input.split('\n')
    var mealCost = parseFloat(inputArray[0]);
    var tip = parseInt(inputArray[1])/100;
    var tax = parseInt(inputArray[2])/100;
    var total = (mealCost * tip) + (mealCost * tax) + mealCost;
    var rounded = Math.round(total)
    console.log("The total meal cost is "+rounded+ " dollars.");
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});