Day 2: Operators =>{ HackerRank-30 Days of Code in C}

Adarsh SAdarsh S
3 min read

Make Sure you read the problem description here.

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.

I'm diving deep into the problem here. I'm assuming you have a basic understanding of C language.

In a nutshell, our job is to write a function which will return the total cost of a meal considering tip and tax percentage.

void solve(double meal_cost, int tip_percent, int tax_percent) {
//write solution here
}

The function doesn't return anything, it means we have to write printf() inside the function.

In the function description - double meal_cost, int tip_percent, and int tax_percent are function parameters.

In the original problem description, they warned us about using precise values.

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

So, we are going to use double instead of float to get greater precision as we are dealing with fractional values. Because the precision value of double is up to 15 but it is up to 7 for float.

  1. Finding Tip

     double tip = (meal_cost * tip_percent) / 100;
    
  2. Finding Tax

     double tax = (tax_percent * meal_cost) / 100;
    
  3. Calculating Total

      double total = meal_cost + tip + tax;
    

we will come back to this problem after learning about a simple concept. Don't lose track.


Round()

Now comes the most important part. We have to round the total to the nearest integer. For that, we use round() function from math.h

round() takes a single argument and converts it into its nearest integer.

Example:

double num = 3.7;
double roundedNum = round(num); //output is 4.000000

The round() function returns a floating-point value, which represents the input value rounded to the nearest integer. By casting the result to an integer using (int), we can truncate the decimal portion of the floating-point value and convert it to an integer.

double num = 3.7;
int roundedNum = (int)round(num); //output is 4

coming back to our problem, let's round up our total cost and truncate those floating point values.

int roundedTotal = (int)round(total);

That's it. Now, all we have to do is print it.

printf("%d", roundedTotal);

Here is the complete code.

void solve(double meal_cost, int tip_percent, int tax_percent) {
/*use double instead of float because it gives a greater precision. Do multiplication before division*/
    double tip = (meal_cost * tip_percent) / 100;
    double tax = (tax_percent * meal_cost) / 100;

    double total = meal_cost + tip + tax;

/*typecasting to (int) to ensure that printf will print        total without any trailing zeroes.
round() used to round of to the nearest integer. it is imported from math.h. */
    int roundedTotal = (int)round(total);

    printf("%d", roundedTotal);

}

Don't copy-paste the code. Try to understand the code and write it on paper first.

0
Subscribe to my newsletter

Read articles from Adarsh S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adarsh S
Adarsh S

24-year-old Alien trying out the human experience as a cyclist/coder.