Saturday, 25 March 2017

Hackerrank Solution For Day 2: Operators

Problem:- Write a Hackerrank Solution For Day 2: Operators or Hacker Rank Solution Program In C++ For " Day 2: Operators " or Hackerrank solution for 30 Days of Code Challenges.


Hackerrank Solution For Day 2: Operators

Logic:- Here we go for Day 2: Operators solution. We can solve this problem by taking a simple example. Suppose you are in cafeteria and you order meal after you finish your meal you have to pay some amount of money also 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 cafeteria. This is a problem for that we will take hacker rank example and solve the problem step by step.

Check:- Hackerrank Solution For Day 0: Hello, World

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 Day Day 1: 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

Solution:- Method 1:-

#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:-

#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;
}


Output:-


Hackerrank Solution For Day 2: Operators

You May Also Like:-










10. Hackerrank Solution For Day 12: Inheritance


No comments:
Write comments

Recommended Posts × +