Whats wrong with my ATM ?

atm
c
easy
practice
wrong-answer

#1

well, I have tested all the examples mentioned in the problem description(which work all right on my computer using gcc 4.8.2) but when I upload and hit submit, it says running and then suddenly WRONG ANSWER with a cross, not sure whats wrong with my code, you may see it below:

#include <stdio.h>

int main(void)
{
    int amount; 
    float balance;
    scanf ("%i %f", &amount, &balance);
    float ans = balance;
    if (amount % 5 == 0 && amount < balance)
        {
         ans = balance - (amount + 0.50);
        }

    printf ("%.2f", ans);

return 0;
}

#2

You are checking for
“amount < balance”
Instead, you should check for:
“amount + 0.5 < balance && amt%5==0”
this is because the sum, along with the extra 0.50 is required to do the transaction.


#3

The amount 0.50 is also to be deducted from the balance.

So, the condition amount < balance should be amount <= (balance-0.5)


#4

Try this case 20 20.1


#5

Well, how do i correct the error in this ?


#6

Sorry guys if I deleted some comment, when I converted this to answer, but I think this is a valid answer :wink:


#7

deleted comment: “Hmmmm, thats gives negative 0.40, but why’s that ? my codes completely optimised for that, is it because I’m comparing an int(amount) with a float(balance) or is it some other reason ? (nibnalin)”


#8

No, every good language can compare integers with real numbers correctly…


#9

Read the question again and check the if condition in your code.