ATM problem C++ help

I have submitted by code 3 times but codechef still says my answer is wrong, I have tested it multiple times and I cannot seem to find whats wrong, if someone could point this out it would really help me:

const double fee = 0.50;
double balance = 0;
int withdraw_amount = 0;




int main()
{
cin >> withdraw_amount >> balance;

if (withdraw_amount % 5 != 0)
	{
		cout << balance << endl;
	}
else if (withdraw_amount > balance)
	{
		cout << balance << endl;
	}
else
	{
		balance -= withdraw_amount + fee;
		cout << setprecision(2) << fixed << balance << endl;
	}



return 0;
}

@az3az09_3134

Brother your code gives WA for test case

 30 30.25
2 Likes

you should use && operator to combine the the if and else if statement because here both the conditions need to be followed but not one amongst them.

1 Like

use this code. i got my answer correct.

#include
#include
using namespace std;
int main()
{
float y;
int x;
cin>>x;
cin>>y;
if(x>=0&&x<=2000&&y>=0&&y<=2000)
{ if((x+0.50)<y&&x%5==0)
y=y-x-0.50;
}cout<<y;
}

2 Likes

using namespace std;
int main()
{
int amt, bal ;
cin>>amt;
cin>>bal;
if (amt % 5 == 0)

{

    bal = bal - amt - 0.5;

    cout<<bal;}

else
    cout<<bal;

}

hope this will help you

your code is also giving wrong ans…
any std lib other than iostream is needed??

This works too.

#include
using namespace std;

int main() {
// your code goes here
int amount;
float balance;
float charge = 0.5;
cin>>amount>>balance;
if(amount >0 && amount%5 ==0 && balance>=(amount + charge) ){
cout<<(balance - (amount+charge));
}
else
{
cout<<balance;
}
return 0;
}

Try this solution, accepted
#include
#include
using namespace std;

int main() {
// your code goes here

int n;
cin>>n;
float n1;
cin>>n1;






if((n%5==0) && ((n+0.5)<n1) &&(n!=0))
{ float r=n1-(n+0.5);
    cout<<fixed<<setprecision(2)<<r;
}

else
cout<<fixed<<setprecision(2)<<n1;

return 0;

}

Try this:

 #include <bits/stdc++.h>
    using namespace std;
    int main(){
        int X;
        float Y;
        const float wdChrg = 0.50;
    cin>>X;
    cin>>Y;
    int t=((X>0)&&X<=2000);
    int u=((Y>=0)&&(Y<=2000));
    int v=(Y>=X+0.5);
    int w=((X%5)==0);
        if ((t==1)&&(u==1)&&(v==1)&&(w==1)){
            cout<<fixed<<setprecision(2)<<(Y-X)-wdChrg;
        }
        else if (v!=0){
            cout<<fixed<<setprecision(2)<<Y;
        }
        else if (w!=0){
            cout<<fixed<<setprecision(2)<<Y;
        }
        else if (t!=0){
            cout<<fixed<<setprecision(2)<<Y;
        }
    }

#include
using namespace std;

int main() {
long long withdrowl_amount;
double balance,initial_balance;
cin>>withdrowl_amount>>initial_balance;
if((withdrowl_amount%5==0)&&(withdrowl_amount<=initial_balance-0.5)){
balance=initial_balance-(withdrowl_amount+0.5);
cout<<balance;
}
else
cout<<initial_balance;
return 0;
}// This is Correct Programm

Simple C code as in yours your if and else if conditions are not proper
#include<stdio.h>

int main()
{
int w;
float t;
scanf("%d%f",&w,&t);
if(w+0.5>t)
{
printf("%.2f",t);
}
else if(w%5!=0)
printf("%.2f",t);
else
printf("%.2f",t-w-0.5);

return 0 ;

}

You are almost correct. You just need to make a small change in else if block,
Because your code won’t work correctly if i gave 50 50, then balance would be in negative 0.50.
try this :

else if (withdraw_amount >= balance && balance >= withdraw_amount+0.50)
{
cout << balance <<endl;
}
else
{
balance -= withdraw_amount + fee;
cout << setprecision(2) << fixed << balance << endl;
}

Many of us ignore the case where the withdraw amount is 0, this code of mine got right answer you can try this if you are interested

#include<bits/stdc++.h>
using namespace std;
int main(){
int cash;
float bal;
cin >> cash >> bal;
float ans = (bal - cash - 0.50);

if ((cash + 0.50) <= bal && cash % 5 == 0){
	if (cash > 0 && cash <= 2000 && bal >= 0 && bal <= 2000){
			cout << fixed << setprecision(2) << ans << endl;
	}
}
else if (cash == 0){
	cout << fixed << setprecision(2) << bal << endl;
}
else{
cout << fixed << setprecision(2) << bal << endl;
}

return 0;

}