ATM: limits 0-2000 how to do that?


Doing the ATM (beginner challenge).
Do I have to implement limits so that “Pooja” cannot withdraw more than 2000 ? When i read the description of the “Input” it says so. But I don’t know if i must do that or not?


int main()
	int withdraw;
	float balance, p;

	scanf_s("%d", &withdraw);

	scanf_s("%f", &balance);

	if ((withdraw % 5 == 0) && (balance >= (withdraw + 0.5))) {
		p = (balance - (withdraw + 0.5)),
			printf("%f", p);
	else {
		printf("%f", balance);

	return (0);


Considering this as your problem, if that’s not the case please comment.

See the question it clearly states that, the transaction will only occur when the amount available in the bank is greater than the amount entered and the bank charges 0.5 fee, so this gives us our first condition that if the amount entered + 0.5 is greater than the amount available in the account.

There is one more contraint that amount entered must be a multiple of 5 so this is our second case.

Hope this helps! for any further clarification you can also see my solution.

1 Like
I interpret it that i must limit in the code that he cannot enter numbers lower than 0 nor higher than 2000?

No, it means that he is limiting himself to entering numbers from 0 to 2000. You DONT have to do anything, its just to help you guess which data type to use, which algorithm to use.

Perfect, thanks! Thats just what i was wondering!

I got the correct answer i searched for by @vijju123 so thanks anyway.

Another question… I submitted it and got wrong answer. I played around with it and it seems that if i enter “Balance = 2000” and “Withdraw = 1999” i get some really weird answer. So it seems the code works between like 3 and 1997. But the area 0-3 and 1997-2000 seems to be broken… Help? :frowning:

Of course, looking at your code.

Remove those printf statements printing the sentence!! Its a machine which checks if your output is exactly equal to expected one or not. If you print these sentences like “Enter the numbeR” ,it will give WA. So thats the first thing.

Then, try to print output upto 2 decimal places ALWAYS. Else this Q gives a WA. And lastly, you are taking input in wrong order. It first gives withdrawl, then balance. You are doing the reverse, first taking balance and then withdrawl.

EDIT: I removed the %.2 and changed the variable to a float type instead. Now it got AC buy codechef! :smiley:

Okey… I have removed the printf sentences. And I have changed order so that Withdrawl comes before Balance.

But I don’t see what you mean with printing 2 decimal places…? I have “.2f" and ".2d” during the printf’s. Can you explain further?

I have updated my post with my new code!

I am not sure if i recall correctly, but this Q gave you WA if your printed stuff like 125.5 instead of 125.50 . Meaning, it required you to print upto 2 decimal places compulsarily.