In: Computer Science
Problem Statement
In a restaurant, if you were pleased by the waiter's service, you may leave him a tip -- you pay him more than the actual value of the bill, and the waiter keeps the excess money. In some countries, not leaving a tip for the waiter is even considered impolite. During my recent holiday I was having dinner in a foreign restaurant. The pamphlet from my travel agency informed me that the proper way of tipping the waiter is the following:
Clearly, sometimes there may be multiple "correct" ways of settling the bill. I'd like to know exactly how many choices I have in a given situation. I could program it easily, but I was having a holiday... and so it's you who has to solve this task. You will be given:
Write a function that computes how many different final sums satisfy the conditions above.
Constraints
Examples
4 100 Returns: 0
4 isn't a round sum, and 5 is too much.
23 100 Returns: 1
The only correct choice is to pay 25 dollars, thus leaving a tip of 2 dollars.
23 24 Returns: 0The same bill, but I don't have enough money to leave an appropriate tip.
220 239 Returns: 1This time, it is appropriate to pay either 235 or 240 dollars. Sadly, I don't have enough money for the second possibility.
1234567 12345678 Returns: 14440A large bill, but with that much money I don't care.
1880000000 1980000000 Returns: 210527
171000000 179999999 Returns: 0
Given Function
int possible_payments(int bill, int cash) {
// fill in code here
}
Answer the problem statement by completing the
Given Function. Follow the constraints and
examples.
The code is written in C if you want you can also compile it in c++ using the required headers. Comments are included in the code. If you require any clarifications please ask in the comments. Thank you.
Note: Go through the code screenshot to understand the indentation in a better way.
THE CODE:
#include<stdio.h>
#include<stdlib.h>
//Function to calculate the number of ways
long int possible_payments(long int bill, long int cash) {
//Calculate the number to remaining to get to the nearest multiple of 5
int rem = (5-bill%5);
bill+=rem;
//Varibale to calaculate the tip
long int tip = rem;
//Varible to store the number of ways
long int ways = 0;
double percentage = 0.0;
//This loops till the amount of bill gets higher than the amount of cash available
//At each iteration inreases the tip and the bill to the next multiple of 5
while(bill<=cash){
//Calculates the percentage of tips in the total bill
percentage = ((double)tip/(double)bill)*100;
if(percentage>=5 && percentage<=10){
ways++;
}
if(percentage>10){
return ways;
}
tip+=5;
bill+=5;
}
return ways;
}
void main(){
//The long int is given in order tor deal with very large numbers. You may use int of necessary.
long int bill;
long int cash;
long int numberofways = 0;
//Takes the bill and cash as input
printf("Enter the bill and the cash: ");
scanf("%ld", &bill);
scanf("%ld", &cash);
//Calls the function to calculate the number of ways
numberofways = possible_payments(bill,cash);
//Display the number of ways
printf("The number of ways are: %ld\n", numberofways);
}
CODE SCREENSHOT
OUTPUT: