Monday, March 2, 2015

Armstrong Number Checker in PHP

In this short tutorial I would like to share with you sample program to check whether the given number by the user is an armstrong number of not. What our program will do is to ask the user to enter a number and then the user can select the find armstrong number by click the button. Our sample program will also check if the user has entered number or not by using PHP character validation functions. I hope you will find my work useful in your learning in PHP programming.

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> Armstrong Number Checker</title>
<style>
body { 
  font-size:25px; 
  font-family:arial;
  color:blue;
  } 
  
#banner {
  width: 750px;
  background-color: yellow;
  height: 100px;
  margin: 0 auto;
  position: inherit;
  top: 100; 
  left: 0; 
  bottom:0; 
  right: 0;
  padding: 16px 16px;
  
  border-radius: 75px;
  border: 5px solid #fba827;
}
  /* BUTTONS */

#submit {
  background: #0a7d66;
  background-image: -moz-linear-gradient(#0fb493, #0a7d66);
  background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #6cbb6b),color-stop(1, #0fb493));
  
  margin: 0 0 0 2px;
  padding: 15px 20px;

  border-radius: 3px 70px 70px 3px;
  -moz-border-radius: 3px 70px 70px 3px;
  border-color: #0fb493 #0da284 #0c9075;

  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;  

  font: 16px 'Droid Serif', serif;
  background: #0fb493;
  color: blue;
  cursor: pointer;

  text-shadow: 0 5px 0 rgba(255,255,255,0.4);
}

#submit:hover {       
    background-color: #0fb493;
    background-image: linear-gradient(#0a7d66, #0fb493);
}   

#submit:active {       
    background: #0fb493;
    outline: none;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;        
}

#submit::-moz-focus-inner {
       border: 0;  /* Small centering fix for Firefox */
}
.inputs {
    -webkit-border-radius: 20px 3px 3px 20px;
    -moz-border-radius: 20px 3px 3px 20px;
    -ms-border-radius: 20px 3px 3px 20px;
    -o-border-radius: 20px 3px 3px 20px;
    border-radius: 20px 3px 3px 20px;

    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -ms-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
    background: yellow;
    border: 5px solid #fba827;
    color: blue;
    font: 22px 'Droid Serif', serif;
    margin: 0 0 10px;
    padding: 15px 20px 15px 20px;
    width: 150px; 
}
</style>
<?php
error_reporting(0);
$values = $_POST['value'];

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

}

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

$title = "======= The Result =======";
    if (ctype_alpha($values)) {
      echo "<script>";  
  echo "alert('PLEASE ENTER A NUMERIC VALUE.');";
  echo "</script>";
  $values="";
  $title=""; 
           $results="";
  $mess=" ";
 }
    else  if ($values=="") {
      echo "<script>";  
  echo "alert('It Cannot Be Empty!!! Please enter a integer value.');";
  echo "</script>";
  $title=""; 
  $values="";
           $results="";
  $mess=" ";
 }
   }  


?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
   <div id="banner">
          <h1 align="center">Armstrong Number Checker</h1>
      </div>
  <br>
<form action="" method="post">
 Enter a Number : <input id="text" type="text" class="inputs"  type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=5 required/>
   <input  id="submit"  type="submit"  name="check" value="Check Armstrong Number" 
  title="Click here check if the given number is an armstrong."/>
  <input id="submit"  type="submit" name="clear" value="Clear" 
  title="Click here to clear text box and values on the screen"/>
</form>

<?php 
 echo $title;
echo "<br>";

 $temp=$values;
  while($temp!=0)
     {
 $rem=$temp%10;
 $sum=$sum+$rem*$rem*$rem;
 $temp=$temp/10;
    }
 if($values==$sum)
  {
$mess = "is a Armstrong Number.";
$results = $values. " ".$mess;
}
else
{
$mess = "is Not an Armstrong Number.";
$results =  $values." ".$mess; 
}
 echo '<br>';
 echo $results;

 ?>
  </body>
</html>

No comments:

Post a Comment