Write a code to find the Hackerrank Day 2 Solution in C programming language with the complete explanation, solution code, and output of the code in details. 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 for understanding a complete question.
Assume you are also interested to pay a Tip(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.
As we are taking hacker rank example so Meal cost is 12 dollar (according to hacker rank) Tip per cent 20 and tax per cent is 8 so for according to above input our program perform 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 Dollar paid by the customer in the cafeteria.
Download the day 2 operators problem statement in pdf and read it before submit your solution it's a best practice to learn more effective any programming language. After that, you can visit official day 2 hackerrank solution submit page and submit your solution over there. Submit Your Hackerrank Day 2 Solution in C Here:- Click Here
Assume you are also interested to pay a Tip(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.
Hackerrank Day 2 Solution Explanation
As we are taking hacker rank example so Meal cost is 12 dollar (according to hacker rank) Tip per cent 20 and tax per cent is 8 so for according to above input our program perform 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 Dollar paid by the customer in the cafeteria.
Also Check:- Day 1 Data Types Hackerrank Solution in C
Download the day 2 operators problem statement in pdf and read it before submit your solution it's a best practice to learn more effective any programming language. After that, you can visit official day 2 hackerrank solution submit page and submit your solution over there. Submit Your Hackerrank Day 2 Solution in C Here:- Click Here
Updated Solutions: Day 2 Operators Solution in C, C++, and Java Programming Language
Visit- Day 2 Operators Solutions C++ and Java Languages
Hackerrank Day 2 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;
}
0 Comments: