ATM: Getting wa

,

#include <stdio.h>

int main()
{
	int i;
	float bal=120;
	//printf("Enter the amt to be withdrawed");
	scanf("%d",&i);
	if((i%5)==0)
	{
		if(i<bal)
		      bal=bal-i-0.5;
			
	}
	printf("%.2f",bal);
	return 0;
}

it return incorrect output for

200 300.00

sir,I ran the output for 200.It was correct.The if statement will definitely prevent it.Right??

There are two numbers in input X and Y, read the statement again :wink: It’s a bit confusing that Y is always 120.00 in examples… In my input Y is 300.00 (I didn’t have decimal numbers in example, fixed).

try 300.00 300.00
And also both “i” and “bal” are input… Read it again… :wink:

@sabarish :: There are a couple of mistakes in your code.

(1).You are only taking one input instead of two.
The program should asks two inputs -

X=Amount pooja wants to withdraw from an ATM.
Y=Balance in the atm.

Your program is only taking the input X (scanf("%d",&i)) but it is not taking the input Y(You have fixed the bal value to 120 ,every time your program assumes that pooja has 120 USD as balance in ATM and yes this  one of the two reasons behind Wrong Answer.

So take balance as input too.

(2).Read this statement carefully

A transaction is possible if Pooja’s account balance has enough cash to perform the withdrawal transaction (including bank charges and bank charges is 0.50 $US.)

So your condition if(i<bal) is wrong .Imagine the case- pooja has 20 dollar balance in ATM and she want to withdraw xactly 20 dollar.Then atm won't allow pooja to make transaction because  the bank can't charges pooja 0.50 $US .(So For successful transaction in this cases ,minimum balance should be 20.50 US $ (as 20$+0.50 $ bank charges=20.50$ .)).But your program allow pooja to make transaction under such case.

So , correct the condition in your code.

#include <stdio.h>

int main()
{
	int i;
	float bal=120;
	//printf("Enter the amt to be withdrawed");
	scanf("%d",&i);
	scanf("%f",&bal);/*You are not taking balance as input*/
	if((i%5)==0)
	{
		if(i+.5<=bal)/*The condition  if(i<bal) was not correct*/
			bal=bal-i-0.5;

	}
	printf("%.2f",bal);
	return 0;
}
1 Like