ATM help getting wrong answer

import java.util.*;

/**
*

  • @author Nitish
    */
    class Atm {

    public static void main(String[] args)
    {
    // TODO code application logic here
    Scanner in=new Scanner(System.in);
    int y=in.nextInt();
    double balance=in.nextDouble();
    //System.out.println(y+" "+balance);
    if(y%5==0 && y<balance)
    {
    balance = balance - y-0.50;
    System.out.println(balance);
    }
    else if(y>=balance)
    {
    System.out.println(balance);
    }
    else
    {
    System.out.println(balance);
    }
    }
    }

what is the problem in ans it’s showing me wrong answer??

Try input

30 30.20

:wink:

There are a couple of bugs in your code

(1)Did you read this line carefully in the problem statement :Output the number with two digits of precision.?

Your code block System.out.println(balance) is not printing the output in two digit precision.
So replace the above line with

System.out.printf("%.2f\n",balance);//Prints output in two digits precesions

(2)The if conditions used is wrong

	    if(y%5==0 && y<balance)
		{
			balance = balance - y-0.50;
			System.out.println(balance);
		} 

I have removed the bug in your java code,now it will get accepted.

    import java.util.*;
	 
	class  Atm
	{
	 
	public static void main(String[] args)
	{
			// TODO code application logic here
			Scanner in=new Scanner(System.in);
			int y=in.nextInt();
			double balance=in.nextDouble();
			//System.out.println(y+" "+balance);
			if(y%5==0 && (y+.50)<=balance)
			{
			balance = balance - y-0.50;
			}
			System.out.printf("%.2f\n",balance);
			}
	} 

PS.Don’t use scanner for taking input in java ,please use BufferedReader as it is several times faster than scanner.
In this problem ,it didn’t cause problem but will trouble you in those problem that has large input file

2 Likes

import static java.lang.Math.abs;
import java.util.Scanner;
class Sample1 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double per = scan.nextDouble();
float s =0.50f;
double a=120.00,b;
if((per%5)==10){
b = abs(per-120.00);
System.out.println(b-(s));
}
else{
System.out.println(a);
}

}

}

Output formatting is not an issue here, see my submission - CodeChef: Practical coding for everyone. I’m not a fan of “look, this is working solution” approach, I think it’s better to show counter example and let them find own bugs…

CAN ANYONE TELL ME PLEASE WHY I AM GETTING WRONG ANSWER?

#include
#include
using namespace std;
using std::setprecision;

int main()
{
int a;
float b;
cin>>a;
if(a>0)
{
cin>>b;
cout<<"\t";
if(b>0||b>0)
{
if(a%5==0)
{
if(b>a)
{
cout<<setprecision(2)<<(b-a-0.5);
}}
else
cout<<b;
}
}
return 0;
}