Decimal Number Conversion Program in PHP

Hi People

This PHP Application is for generating a Decimal Number Conversion Table to Binary, Hexadecimal and Octal Numbers.

In this we have a HTML form where the user need to enter the Starting and the Ending Decimal Number for which she wants to generate a Number Conversion Table. 

This Numbers are send as parameters in a PHP page which produces the Table.

PHP Math Functions

decbin(), dechex(), decoct() have been used for doing the conversion.

NumberConversionForm.html

<html>
<head>
<title> Table Generator-Decimal Number to Binary, Hexadecimal and Octal </title>
</head>

<body>

<h2><u><font color="Blue" face="verdana"> Table Generator- Decimal Number to Binary, Hexadecimal and Octal </font></u></h2>
<form action="NumberConversion.php" method="post">

<table border="0">

<tr>
<td> <b>Enter the Starting Decimal No: <br/>
*Starting No should be smaller than Ending No.</b> </td>
<td> <input type="textbox" name="startNo"> </td>
</tr>

<tr>
<td> <b>Enter the Ending Decimal No: </b> </td>
<td> <input type="textbox" name="endNo"></td>
</tr>

 <tr>
 <td colspan="2" align="center"> <input type="submit" value="Submit"> </td>
 </tr>
</table>

</form>
</body>
</html>

NumberConversion.php- The PHP Program which does all the work

<html>
<head>
	<title> Table-Decimal Number to Binary, Hexadecimal and Octal</title>

	<style type="text/css">
	/*Style for Table*/

	table
	{
	font-family:Arial; font-size:15px;

	color:navy; font-weight: bold;

	border: 1px solid green;

	text-align: center;

	border-collapse:collapse; width:30%;
	}

	th
	{
	font-family:Arial; font-size:16px;

	color:black; font-weight: bold;

	text-align: center;
	}

	td
	{
	height:35px;
	}
	/*Style for The Heading*/

	h2
	{
	font-family:Arial; font-size:20px;

	color: green; font-weight: bold; text-align: center;

	text-decoration:underline
	}

</style>
</head>

<body>
<?php

$startNo = $_POST['startNo'];
$endNo = $_POST['endNo'];

$counter = 0;
if($startNo > $endNo)
{
	echo "Please Enter Correct numbers <br>";

}

else
{
	echo "<table border='1' align='center' bgcolor='#E0FFFF'>";
	echo "<tr>";
	echo " <td align='center' colspan='4' bgcolor='#7FFFD4'><h2>Decimal Number Convertor Table</h2></td>";
	echo "</tr>";

   echo "<tr>";
   echo "<th>Decimal Number</th>";
   echo "<th>Binary Number</th>";
   echo "<th>Hexadecimal Number</th>";
   echo "<th>Octal Number</th>";
   echo "</tr>";

for ($counter = $startNo; $counter <= $endNo; $counter++)
{
  echo "<tr>";
  echo "<td>".$counter."</td>";
  echo "<td>".decbin($counter)."</td>";
  echo "<td>".dechex($counter)."</td>";
  echo "<td>".decoct($counter)."</td>";
  echo "</tr>";
}

echo "</table>";
}
?>
</body>
</html>

Snapshot of the o/p

Thats it. A simple PHP Program showing the power of PHP. 😀

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