Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

Hacker Rank: Day 2: Operators Solution in PHP


Hacker Rank: Day 2: Operators Solution in PHP

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

Example:

meal cost = 100

tip percent = 15

tax percent = 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:

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, meal cost(the cost of the meal before tax and tip). 

The second line has an integer, tip percent(the percentage of meal cost being added as tip). 

The third line has an integer, tax percent(the percentage of meal cost being added as tax).

Sample Input

12.00

20

8

Sample Output

15

Explanation:

Given: 

meal_cost = 12, tip_percent = 20, tax percent = 8

Calculations: 

tip = 12 and 12/100x20=2.4

tax = 8 and 8/100x20=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.

PHP Solution:

function solve($meal_cost, $tip_percent, $tax_percent) {

    $tip = $meal_cost/100*$tip_percent;

    $tax = $meal_cost/100*$tax_percent;

    $total = $meal_cost + $tip + $tax;

    print round($total);
}

$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%lf\n", $meal_cost);

fscanf($stdin, "%d\n", $tip_percent);

fscanf($stdin, "%d\n", $tax_percent);

solve($meal_cost, $tip_percent, $tax_percent);

fclose($stdin);

 

Tags : hackerRank, PHP,

Views : 645

Subscribe Us


Follow Us