DEV Community

Cover image for CodeChef - ATM Problem
Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on

CodeChef - ATM Problem

CodeChef ATM Problem- Problem Code: HS08TEST

Basic Programming Series....

Problem Statement:- Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction.

Input

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.

Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.

Output

Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.

Explanation

Entered amount must be multiple of 5 that's why we are checking it by using modulus operator which returns the remainder of the numbers after dividing it with 0, if the remainder is 0 then it is multiple of 5. Second condition is to check whether the withdraw amount is available in her bank account after adding the 0.50$ charges, If both the condition are true then this expression is executed "balance = balance-amount-charges". If both condition doesn't satisfy then the available balance needs to be printed.

Also the output is required number with two digits of precision i.e balance must be shown with two decimal points. for that the below printf statement is used to format the output as per our need.

if(amount%5==0 && amount+charges<=balance) {
            balance = balance-amount-charges;
Enter fullscreen mode Exit fullscreen mode
System.out.printf("%.2f", balance);
Enter fullscreen mode Exit fullscreen mode

Example - Successful Transaction

Input:

30 120.00
Enter fullscreen mode Exit fullscreen mode

Output:

89.50
Enter fullscreen mode Exit fullscreen mode

Example - Incorrect Withdrawal Amount (not multiple of 5)

Input:

42 120.00
Enter fullscreen mode Exit fullscreen mode

Output:

120.00
Enter fullscreen mode Exit fullscreen mode

Example - Insufficient Funds

Input:

300 120.00
Enter fullscreen mode Exit fullscreen mode

Output:

120.00
Enter fullscreen mode Exit fullscreen mode

Check output here:- CLICK HERE

Link to the Series:- CLICK HERE

Link to original problem https://www.codechef.com/problems/HS08TEST

LIKE! SHARE! COMMENT!

More problems will be added regularly to this series.

Discussion (0)