Day 2: Operators|| hackerrank || 30 days of cod || programming_info

Objective
In this challenge, you'll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!

Input Format

There are  lines of numeric input:
The first line has a double,  (the cost of the meal before tax and tip).
The second line has an integer,  (the percentage of  being added as tip).
The third line has an integer,  (the percentage of  being added as tax).

Output Format

Print the total meal cost, where  is the rounded integer result of the entire bill ( with added tax and tip).

Sample Input

12.00
20
8

Sample Output

15

Explanation

Given:

Calculations:



We round  to the nearest dollar (integer) and then print our result, .

Now here's the solution in C/C++/JAVA/ JS:-

code snippet:
using c language:
 #include <stdio.h>
 #include <math.h>
 int main()
 {
   double mealCost;
   int  tipPercent;
   int taxPercent;
    scanf("%lf", &mealCost);
    scanf("%d", &tipPercent);
    scanf("%d", &taxPercent);
    double tip = mealCost * tipPercent / 100;
    double tax = mealCost * taxPercent / 100;
    int totalCost = (int) round(mealCost + tip + tax);
    printf("The total meal cost is %d dollars.", totalCost);
    return 0;
}

using c++ language:
#include <bits/stdc++.h>
using namespace std;
// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent) 
    {
    double extra,sum;
    extra=(meal_cost*(tip_percent+tax_percent))/100;
    sum=meal_cost+extra;
    cout<<round(sum)<<endl;
    }
int main()
    {
        double meal_cost;
        cin >> meal_cost;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        int tip_percent;
        cin >> tip_percent;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        int tax_percent;
        cin >> tax_percent;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        solve(meal_cost, tip_percent, tax_percent);
        return 0;
    }


using java language:

import java.util.*;
public class Arithmetic {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double mealCost = scan.nextDouble(); // original meal price
        int tipPercent = scan.nextInt(); // tip percentage
        int taxPercent = scan.nextInt(); // tax percentage
        scan.close();
        // Calculate Tax and Tip:
        double tip = mealCost * tipPercent / 100;
        double tax = mealCost * taxPercent / 100;
        // cast the result of the rounding operation to an int and save it as totalCost 
        int totalCost = (int) Math.round(mealCost + tax + tip);
        System.out.println("The total meal cost is " + totalCost + " dollars.");
    }
}

using javascript language:

'use strict';
var _input = '';
var _index = 0;
process.stdin.on('data', (data) => { _input += data; });
process.stdin.on('end', () => {
 _input = _input.split(new RegExp('[\n ]+'));
 main(+(_input[0]), +(_input[1]), +(_input[2]));    
});
function main(mealCost, tipPercent, taxPercent) {
    let tip = mealCost * tipPercent / 100;
    let tax = mealCost * taxPercent / 100;   
    let totalCost = Math.round(mealCost + tax + tip); 
    console.log("The total meal cost is " + totalCost + " dollars.");
}
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support