Algorithm


Problem Name: 30 days of code - Day 2: Operators

Problem Link: https://www.hackerrank.com/challenges/30-operators/problem?isFullScreen=true

In this HackerRank in 30 Days of Code - Day 2: Operators problem solution,

Objective
In this challenge, you will 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. Round the result to the nearest integer.

Example

mealcost = 100

tippercent = 15

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();

// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent) {
    double tip_cost= meal_cost * tip_percent / 100;
    double tax_cost= meal_cost * tax_percent / 100;
    double total_cost= (meal_cost + tip_cost + tax_cost);
    printf("%.0lf",total_cost);
}

int main()
{
    char* meal_cost_endptr;
    char* meal_cost_str = readline();
    double meal_cost = strtod(meal_cost_str, &meal_cost_endptr);

    if (meal_cost_endptr == meal_cost_str || *meal_cost_endptr != '\0') { exit(EXIT_FAILURE); }

    char* tip_percent_endptr;
    char* tip_percent_str = readline();
    int tip_percent = strtol(tip_percent_str, &tip_percent_endptr, 10);

    if (tip_percent_endptr == tip_percent_str || *tip_percent_endptr != '\0') { exit(EXIT_FAILURE); }

    char* tax_percent_endptr;
    char* tax_percent_str = readline();
    int tax_percent = strtol(tax_percent_str, &tax_percent_endptr, 10);

    if (tax_percent_endptr == tax_percent_str || *tax_percent_endptr != '\0') { exit(EXIT_FAILURE); }

    solve(meal_cost, tip_percent, tax_percent);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) { break; }

        data_length += strlen(cursor);

        if (data_length  <  alloc_length - 1 || data[data_length - 1] == '\n') { break; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length);

    return data;
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <cmath>
#include <iostream>

using namespace std;

int main() {
    string tmp;

    getline(cin, tmp);
    double mealCost = stod(tmp);

    getline(cin, tmp);
    int tipPercent = stoi(tmp);

    getline(cin, tmp);
    int taxPercent = stoi(tmp);

    double tip = tipPercent * mealCost / 100;
    double tax = taxPercent * mealCost / 100;

    int totalCost = (int) round(tip + tax + mealCost);
    cout<< totalCost;

    return 0;
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming



using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

    public static void solve(double meal_cost, int tip_percent, int tax_percent)
    {
        
        var tip= tip_percent * meal_cost / 100;
        var tax=tax_percent * meal_cost / 100;
       
        var total_cost= Math.Round(meal_cost + tip + tax);
       
        Console.WriteLine($"{total_cost}");
       

    }

}

class Solution
{
    public static void Main(string[] args)
    {
        double meal_cost = Convert.ToDouble(Console.ReadLine().Trim());

        int tip_percent = Convert.ToInt32(Console.ReadLine().Trim());

        int tax_percent = Convert.ToInt32(Console.ReadLine().Trim());

        Result.solve(meal_cost, tip_percent, tax_percent);
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the solve function below.
static void solve(double meal_cost, int tip_percent, int tax_percent) 
{
        double tip=meal_cost*tip_percent/100;
        double tax=meal_cost*tax_percent/100;
        int totalCost=(int)Math.round(meal_cost+tip+tax);
        System.out.print(totalCost);
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        double meal_cost = scanner.nextDouble();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int tip_percent = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int tax_percent = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        solve(meal_cost, tip_percent, tax_percent);

        scanner.close();
    }
}
Copy The Code & Try With Live Editor

#5 Code Example with Python Programming

Code - Python Programming


import math
import os
import random
import re
import sys

#
# Complete the 'solve' function below.
#
# The function accepts following parameters:
#  1. DOUBLE meal_cost
#  2. INTEGER tip_percent
#  3. INTEGER tax_percent
#

def solve(meal_cost, tip_percent, tax_percent):
    # Write your code here
    int(meal_cost)
    int(tip_percent)
    int(tax_percent)
    bill = ( ((tip_percent * meal_cost)/100) + ((tax_percent * meal_cost)/100) + meal_cost )
    print (round(bill))
    return
if __name__ == '__main__':
    
    meal_cost = float(input().strip())

    tip_percent = int(input().strip())

    tax_percent = int(input().strip())

    solve(meal_cost, tip_percent, tax_percent)
Copy The Code & Try With Live Editor
Advertisements

Demonstration


[Solved] Day 2: Operators solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP,  Python in 30 days of code

Previous
[Solved] Day 1: Data Types solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code
Next
[Solved] Day 3: Intro to Conditional Statements solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code