ATM Machine logic in PHP.


ashwin vasava

Q. Consider a hypothetical ATM machine which gives you notes of 10, 20, 50, 100, 500 & 1000 in denomination. If I enter 5,550 Rs. It should give me 5 notes of 1000, 1 note of 500 and 1 note of 50. ATM machines cannot store coins. So, if input amount is something like 1978 then machine should print “Invalid Amount”.

A .

<?php

$amount=$_REQUEST[‘amount’];
$rupees = array(1000,500,100,50,20,10);
$count = array(0,0,0,0,0);

for($i=0;$i<count($rupees);$i++):
if($rupees[$i]<$amount || $rupees[$i]==$amount):
$count[$i]=floor($amount/$rupees[$i]);
$amount=$amount%$rupees[$i];
endif;
endfor;
print ‘<b>Input=</b>’.$_REQUEST[‘amount’].'<br>’;

if($amount==0):
for($i=0;$i<count($count);$i++):

if($count[$i]!=0):

echo ($rupees[$i].’ * ‘.+$count[$i] .’=’. ($rupees[$i]*$count[$i])).'<br>’;
$tot += $rupees[$i]*$count[$i];
endif;
endfor;
print ‘<b>Total=</b>’.$tot;
else:
print ‘<b>Invalid Amount ‘.$_REQUEST[‘amount’].'</b>’;
endif;

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>AIM MACHINE</title>
</head>

<body>
<form name=”frm” method=”post” action=”#”>
Enter value:<input type=”text” name=”amount” value=”” />
<input type=”submit” name=”submit” value=”submit” />
</form>
</body>
</html>

View original post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s