3/25/2017

Day 2: Operators Hackerrank Solution | 30 Days of Code

Problem Statement- Day 2: Operators Hackerrank Solution or Hackerrank solution for 30 Days of Code Challenges. Here we go for Day 2: Operators solution. We can solve this problem by taking a simple example. Suppose you are in the cafeteria and you order a meal after you finish your meal you have to pay some amount of money. 

Assume you are also interested to pay a Tip(extra amount of money almost 15 - 20 percent of your bill), and you also have to pay Tax's so what will be the total amount you paid in the cafeteria. This is a problem for that we will take hacker rank example and solve the problem step by step.

Check:- Solution For Day 0: Hello, World

Day 2: Operators Solution Explanation


As we are taking hacker rank example so Meal cost is 12 dollar (according to hacker rank) Tip percent 20 and tax percent 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 final Bill is 15 Dollar paid by customer in cafetaria..

Also Check:- Hackerrank Solution For Data Types

If you are In INDIA you can take your example in Indian Rupees or you can consider Dollar as a Rupees. There is two way to solve this problem I have written both codes, also check what is the difference between both methods, there is a minor difference. Now copy the colored code and paste into hacker Rank editor and make sure while selecting editor (Always choose C++, not C++14).

Submit Your Solution Here:- Click Here

Updated Solutions: Day 2: Operators Solution in C, C++, and Java Programming Language

Method 1:- Operators Hackerrank Solution


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
double meal,tipp,taxp,total;
cin>>meal>>tipp>>taxp;

tipp=(meal*tipp/100);

//cout<<tipp<<endl;

taxp=(meal*taxp/100);

//cout<<taxp<<endl;

total=round(meal+tipp+taxp);

//cout<<total<<endl;

cout<<"The total meal cost is "<<total<<" dollars.";


return 0;
}

Method 2:- Operators Solution


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
double meal;

int tipp,taxp,total;

cin>>meal>>tipp>>taxp;

tipp=round(meal*tipp/100);

taxp=(meal*taxp/100);

total=meal+tipp+taxp;

cout<<"The total meal cost is "<<total+1<<" dollars.";


return 0;
}

Day 2: Operators Code Output


Day 2: Operators output

Similar on 30 Days of Code

  1. Day 3: Intro to Conditional Statements
  2. Class vs. Instance Solution
  3. Day 5: Loops
  4. Let's Review Hackerrank Solution
  5. Day 7: Arrays

No comments:

Post a Comment