Don't Wait,Do it yourself.

Latest courses

Hackerank Day 2: Operators solution in java

Day 2: Operators

hackerrank day 2, operators 30 days code java solution

Algorithm

Step:-1 Type cast Integer data type to Double. int tip_percent -> double tip_percent, and int                              tax_percent ->double tax_percent.
Step:-2  Calculate tip=(meal_cost*tip_percent)/100 and tax=(meal_cost*tax_percent)/100.
Step:-3  Calculate total_cost=meal_cost+tip+tax.
Step:-4  Round the total_cost as the result,Round(total_cost).

Explanation

Here, three line of input are differents data types- double, integre, and integer and output is rounded value.so, our first aim to convert all into one data types-whether integer or double.But for more efficient, convert into double types. 
In this problem we don't convert, simply type casting from integer to double(preserve all bits).
int tip_percent, int tax_percent are type cast into double.
At first, we calculate result and hold in into total variable where int tip_percent and int tax_percent are type cast into double as (double)tip_percent and (double)tax_percent).
double total=meal_cost+(meal_cost*(double)tip_percent)/100+(meal_cost*(double)tax_percent)/100;
At last, simply output the rounded total value using Math.round() Function.System.out.println(Math.round(total));

Calculation

Step 1:- tip=mealCost * (tip_Percent/100).

Step 2:- tax=mealCost * (tax_Percent/100).

Step 3:- totalCost=mealCost  + tip + tax.

Step 4:- output=round(totalCost).

Examples

Three line input and its output.
Q.no. Input output
1 12.00
20
8
15

Program


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 total=meal_cost+(meal_cost*(double)tip_percent)/100+(meal_cost*(double)tax_percent)/100;
System.out.println(Math.round(total));

}

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();
}
}


1 comment:

Please do not enter any spam link in the comment Box