Tuesday, February 17, 2015

Reverse a Number in PHP

In this article I have a wrote another simple program that uses PHP as my programming language I called this program reverse a number. What this program will do is very simple it will ask the user to enter a series of number digits and then our program will reverse the arrangement of the number let say the user give 12345 our program will convert in 54321. I hope you will find my program useful in learning how to write a program in PHP.

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> Reverse a Number</title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
</style>
<?php
error_reporting(0);

$values = $_POST['value'];

  

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

     if (ctype_alpha($values)) {
      echo "<script>";  
  echo "alert('PLEASE ENTER A NUMERIC VALUE.');";
  echo "</script>";
  $values="";
 }
 
else if(preg_match('#[^a-zA-Z0-9]#', $values)) {
      echo "<script>";  
  echo "alert('PLEASE ENTER A NUMERIC VALUE.');";
  echo "</script>";
        $values="";
      
}

    else  if ($values=="") {
      echo "<script>";  
  echo "alert('It Cannot Be Empty!!! Please enter a integer value.');";
  echo "</script>";
  $values="";
           }
else { 
  $rem =0; $rev =0; 
    $originalnum = $values; 
  
  while($values > 1) { 
      $rem = $values %10; 
      $rev = ($rev * 10)+ $rem; 
      $values = $values/10; 
 } 
 
$title .= " ===== GENERATED RESULT ===== " ."<BR><BR>";
$title .= "Original Number Arrangement  : ". $originalnum."<br><br>"; 
$title .= "Reverse Number  Arrangement  : ". $rev; 

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

?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
  <br> <h1 align="center"> Reverse a Number</h2>
  <br>
<form action="" method="post">
 Enter a Number : <input type="text" name="value" 
           value="<?php echo $originalnum; ?>" autofocus  size=20/>
   <input type="submit" name="check" value="Reverse Number" 
  title="Click here to reverse the number."/>
  <input type="submit" name="clear" value="Clear" 
  title="Click here to clear text box and values on the screen"/>
</form>

<br> <br>

<?php 
echo $title;
 ?>
  </body>
</html>





No comments:

Post a Comment