Tip Calculator
Collapse Content
Show Content
You are working on an app to calculate tips. Given 2 ints: a
, for the purchase amount in cents, and b
, for the quality of service received, can you calculate the tip in cents? Round the answer down to the nearest cent. Calculate the tip according to the following table:
Quality | Percent | decimal |
---|---|---|
1 | 5% | 0.05 |
2 | 10% | 0.1 |
3 | 15% | 0.15 |
4 | 20% | 0.2 |
5 | 25% | 0.25 |
Example Input/Output
Given an input of 1000 3
, return 150
.
Explanation
There was a rating of 3
which according to the above table means you should give a tip of 15%.
1000 * 0.15 = 150.
Java Casting Types
Click for More
Challenge
Calculate the tip as above and return an int
of the tip in cents (rounded down).
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Matt
Nov 18, 1:28 AMCompleted, but won't check off.
Кирилл Варивода
Jan 5, 1:20 PMHa, I was just using this formula working only with int values:
x = (1000*15)/100
David
Mar 2, 8:39 PMJust gotta say that thanks for including quotes, it keeps me somewhat interested and inspired at the same time while going through this awesome noob training. Pura vida!
isnard
Mar 10, 12:37 PMShena
Jan 2, 8:43 AMahahaha :D i don't understand the instructions????
hmpff guess so ... i couldn't figure out why those are the outputs ... -.-
can someone explain the instructions ? hehehe :)
Shena
Jan 2, 8:46 AMahhhh okey got it myself ehehe :D anyways thanks :)
Bernard Mitchell
Jun 1, 12:31 PMdouble f= b*.05; // .05, .1, .15, .2, .25
double c= a*f;
return (int)c;
thales
Jul 15, 10:47 AMIf you just use doubles instead of integers then integers become doubles.
example 5/2=2 but 5/2.0=2.5
my code
Islam Elzohary
Dec 22, 12:49 PMI don't understand the proplem!!!
Karina Nihalani
Mar 11, 4:45 PMI don't understand the problem.
Learneroo
Mar 12, 12:12 PMYou just need to calculate the tip according to the given table. The tip should be a percentage of the the total purchase. I added an example to make it clearer.
Anand Ani
Mar 15, 11:23 AMeasy peoples
import java.util.Scanner;
public class Main {
}
Hanlise
May 14, 11:12 AMdef do_stuff(a, b):
output=int()
thisdict={
1:0.05,
2:0.1,
3:0.15,
4:0.2,
5:0.25
}
output += thisdict.get(b)
x=a*output
return x
my answer using python
Dheeraj Parmar
Aug 4, 12:46 PM#include
using namespace std;
int func(int a , int b){
}
int main()
{
int a,b;
cin>>a; // no of purchase amount:
cin>>b; // the quality of service recieved:
cout<<func(a,b);
}
AMR
Aug 9, 12:26 PMUsing Python - Do you guys think this code is too long and messy?
price_of_meal = float(input("How much did your meal cost: "))
quality_of_service = int(input("Rate the quality of service based from 1 to 5: "))
while quality_of_service < 1 or quality_of_service > 5:
if quality_of_service < 1:
print("Too low")
if quality_of_service == 1:
print(round(price_of_meal * 0.05, 2))
elif quality_of_service == 2:
print(round(price_of_meal * 0.1, 2))
elif quality_of_service == 3:
print(round(price_of_meal * 0.15))
elif quality_of_service == 4:
print(round(price_of_meal * 0.2))
elif quality_of_service == 5:
print(round(price_of_meal * 0.25))
Roger
Aug 16, 12:07 PMconst service = {
1: 0.05,
2: 0.1,
3: 0.15,
4: 0.2,
5: 0.25,
}
function qualityOfService(a, b) {
let tip = a * service[b]
return tip
};
Roger
Aug 16, 1:39 PMconst service = {
1: 0.05,
2: 0.1,
3: 0.15,
4: 0.2,
5: 0.25,
}
function qualityOfService(a, b) {
let tip = a * service[b]
return tip += a
}
console.log(qualityOfService(10, 3).toFixed(2))