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


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment