Write a Program to Day 2 Operators Hackerrank Solution in C programming language with the complete explanation, solution code, and output of the code in detail. This is the third problem of 30 days of code. We can solve this problem by taking a simple example. It's a better idea to take a real-life example so we can understand the problem statement properly.

Suppose you are in the cafeteria and you order a meal after you finish your meal you have to pay some amount of money. This example helps us to solve this (Day 2 Hackerrank Solution in C) programming statement or problem within a minute. So let's dive into understanding a complete question.

Assume you are also interested to pay a Tip(an extra amount of money almost 15 - 20 per cent of your bill), and you also have to pay Taxes. So what will be the total amount you paid in the cafeteria. Now take a hackerrank example and solve the problem step by step.

**Check-**Day 0 Hello, World. Solution in C

Before submitting your solution it's a best practice to learn more effective any programming language. After that, you can visit the official day 2 hackerrank solution submit page and submit your solution over there. Submit Your Hackerrank Day 2 Solution in C Here:- Click Here

## Day 2 Operators Hackerrank Solution in 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>
char* readline();
// 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;
char* meal_cost_str = readline();
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;
char* tip_percent_str = readline();
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;
char* tax_percent_str = readline();
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;
}
char* readline() {
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;
}
```

### Hackerrank Day 2 Operators Explanation

As we are taking the hackerrank example so Meal cost is 12 dollar (according to hacker rank) the Tip per cent is 20 and the tax per cent is 8 so according to the above input our program perform the following steps.

Step:1 Tip = Mealcost * tip percent / 100

Tip = 12 * 20 / 100 = 2.4

Step 2:- Tax = mealcost * tax percent / 100

Tax = 12 * 8 / 100 = 0.96

Step 3:- Total = Mealcost + Tip +Tax.

Total = 12 + 2.4 +0.96 = 15.36

Total = round(Total) = 15. So the final Bill is 15 Dollars paid by the customer in the cafeteria.

**Also Check:**Day 1 Data Types Hackerrank Solution in C

## 0 Comments: