Day 2: Arithmetic Operators - 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 2: Arithmetic Operators - 30 Days Code

 Introduction:

Welcome to another coding challenge! In this task, we will dive into the world of arithmetic operators. Understanding how to perform basic arithmetic operations is fundamental in programming. By the end of this challenge, you'll be comfortable working with meal costs, tip percentages, and tax percentages, and rounding the result to the nearest integer.


The Challenge:

The challenge is to calculate and print the total cost of a meal, considering the base cost of the meal, tip percentage, and tax percentage. The goal is to round the result to the nearest integer.


Format & Sample:

Let's explore the expected format and review a sample to better understand the challenge.


Input Format:

The first line contains a double, meal_cost (the cost of the meal before tax and tip).

The second line contains an integer, tip_percent (the percentage of the meal cost being added as tip).

The third line contains an integer, tax_percent (the percentage of the meal cost being added as tax).


Output Format:

Print the total cost rounded to the nearest integer.


Sample Input:

12.00

20

8


Sample Output:

15


Explanation:

Given:

meal_cost = 12.00

tip_percent = 20

tax_percent = 8


Calculations:

tip = 12.00×20/100=2.40

tax=12.00×8/100=0.96

total_cost=12.00+2.40+0.96=15.36

total_cost=12.00+2.40+0.96=15.36

Rounding to the nearest integer gives 15, which is the expected output.


Code Breakdown:

Now, let's break down the code and understand each section.


Reading Input:

string meal_cost_temp;

getline(cin, meal_cost_temp);

double meal_cost = stod(ltrim(rtrim(meal_cost_temp)));

string tip_percent_temp;

getline(cin, tip_percent_temp);

int tip_percent = stoi(ltrim(rtrim(tip_percent_temp)));

string tax_percent_temp;

getline(cin, tax_percent_temp);

int tax_percent = stoi(ltrim(rtrim(tax_percent_temp)));

In this section, we read the input values for meal_cost, tip_percent, and tax_percent from the standard input.


Printing Output:

void solve(double meal_cost, int tip_percent, int tax_percent) {

    double tip = meal_cost * (tip_percent / 100.0);

    double tax = meal_cost * (tax_percent / 100.0);

    double total_cost = meal_cost + tip + tax;


    // Round the total cost to the nearest integer

    int rounded_cost = round(total_cost);


    // Print the result

    cout << rounded_cost << endl;

}

In the solve function, we perform the necessary arithmetic calculations, including calculating the tip, tax, and total cost. The round function is used to round the total cost to the nearest integer.


Final Code:

#include <bits/stdc++.h>

using namespace std;

void solve(double meal_cost, int tip_percent, int tax_percent) {
    double tip = meal_cost * (tip_percent / 100.0);
    double tax = meal_cost * (tax_percent / 100.0);
    double total_cost = meal_cost + tip + tax;

    // Round the total cost to the nearest integer
    int rounded_cost = round(total_cost);

    // Print the result
    cout << rounded_cost << endl;
}

int main() {
    string meal_cost_temp;
    getline(cin, meal_cost_temp);

    double meal_cost = stod(meal_cost_temp);

    string tip_percent_temp;
    getline(cin, tip_percent_temp);

    int tip_percent = stoi(tip_percent_temp);

    string tax_percent_temp;
    getline(cin, tax_percent_temp);

    int tax_percent = stoi(tax_percent_temp);

    // Call the solve function with the provided inputs
    solve(meal_cost, tip_percent, tax_percent);

    return 0;
}


This code is designed to efficiently calculate and print the total cost of a meal, considering tip and tax percentages. Understanding the usage of arithmetic operators and input/output operations is key to successfully completing this challenge. Feel free to experiment with different inputs to further solidify your understanding. Happy coding!





إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.