Code For Alls..!

get the code!!

Thursday, 14 September 2017

Day 2: Operators-hackerrank-solution

                                                                  Day 2: Operators



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 Formata
Print The total meal cost is totalCost dollars., where  is the rounded integer result of the entire bill ( with added tax and tip).
Sample Input
12.00
20
8
Sample Output
The total meal cost is 15 dollars.
Explanation
Given:
, , 
Calculations:  



We round  to the nearest dollar (integer) and then print our result:
The total meal cost is 15 dollars.

C Program:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
         float mc,tp,tx,tc;
    int tip,tax,l,m;
    scanf("%f\n%d\n%d",&mc,&tip,&tax);
    tp=mc*tip/100;
    tx=mc*tax/100;
    tc=mc+tp+tx;
    l=tc;
    m=(tc*100)-(l*100);
    l=(m>=50)?l+1:l;
    printf("The total meal cost is %d dollars.",l);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}


11 comments:

  1. m=(tc*100)-(l*100);
    l=(m>=50)?l+1:l;
    this line is not understood me,plz understand

    ReplyDelete
  2. if m is greater than or equal to 50 then l+1 is printed else l is printed

    ReplyDelete
    Replies
    1. but whats the urpose of it please explain it in detail.

      Delete
    2. That is used for rounding off purpose

      Delete
  3. tc=mc+tp+tx;
    i=tc;
    I didn't understand that line

    ReplyDelete
  4. =(tc*100)-(l*100);
    l=(m>=50)?l+1:l;
    this line is not understood me,plz understand

    ReplyDelete
  5. =(tc*100)-(l*100);
    l=(m>=50)?l+1:l;

    purpose for these two lines are explained below
    line 1 is used to found that the value of digits after point
    for example in above question
    tc*100=1536
    l*100 =1500 (beacuse tc is converted into int from float)
    thus m=36 (=(tc*100)-(l*100);) after this
    FOR SECOND LINE
    after this a ternary operator is used which will check whether m>=50 or not
    it is used for round off because if
    15.36 it will be 15
    but if
    15.56 it will be 16
    so if m>=50 it will add 1 to l otherwise not.

    HOPE YOU UNDERSTAND.
    IF You want to learn C and other programming language plus quick tricks, daily knowledgeable post in FREE... then just follow us on instagram @techno_jerry


    HAPPY LEARNING

    ReplyDelete