Thursday, February 12, 2015

Decimal To Romal Numeral Converter in PHP

I would like to share with you a simple program that I wrote PHP as my programming language I called this program Decimal To Roman Numeral Converter in PHP. What this program will do is to ask the user to enter any number in integer format and then the user can select convert to roman numeral button to convert the decimal value to its roman numeral equivalent. The code is very short and simple I also added a function if the user give invalid value such as words, letters or even special character our program will not hang or mal function in such situation encounter.

 If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360



Sample Output of Our Program

Program Listing

<html>
<title> Decimal To Roman Numeral Converter</title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
</style>


<?php
error_reporting(0);

$values = intval($_POST['value']);

if(isset($_POST['check'])) {

  if ($values==" ") {
      echo "<script>";  
  echo "alert('It Cannot Be Empty!!! Please enter a number.');";
  echo "</script>";
  $values=" ";
  $results=" ";
     }

 if ($values != " ") {

  $natural_roman = array(1000 => 'M', 500 => 'D', 100 => 'C', 50 => 'L', 10 => 'X', 5 => 'V', 1 => 'I');

    function convert_to_roman($natural)

    {

        global $natural_roman;

        reset($natural_roman);

        while (list($key, $value) = each($natural_roman))

        {

            while ($natural >= $key)

            {

                $natural -= $key;

                $rn .= $value;

            }

        }

        return str_replace(

                                array('DCCCC', 'CCCC', 'LXXXX', 'XXXX', 'VIIII', 'IIII'),        

                                array('CM', 'CD', 'XC', 'XL', 'IX', 'IV'),

                                $rn

                            );        

    }

    
   $results .=  "==== REPORT ==== ";
   $results .=  "<br><br>";
   $results .= "The number " .$values." it's Roman Numeral equivalent is "
               .convert_to_roman($values).".";
   
  
   }
   
    }
       
    

if(isset($_POST['clear'])) {
  $values=" "; 
  $results= " ";
  
}

?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
  <br> <h1 align="center">Decimal To Roman Numeral Converter</h2>
  <br>
<form action="" method="post">
 Enter a Sentence : <input type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=10/>
 <br><br>
   <input type="submit" name="check" value="Convert To Roman Numeral" 
  title="Click here to convert decimal to roman numeral equivalent."/>
  <input type="submit" name="clear" value="Clear" 
  title="Click here to clear text box and values on the screen"/>
</form>
<br>
<?php 
echo $results;
 ?>
  </body>
</html>


No comments:

Post a Comment