DEV Community

Discussion on: Day 2: Operators - 30 Days of Code HackerRank

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

}