Wednesday, February 18, 2015

Power of a Number in PHP

In this program will ask the user to enter the base and exponent of a number a number and then it will compute for the power of the number given by the user. Example if our user will give 5 as its base  and the exponent is 3 the computed result is 125. As simple as 5 * 5 *5 we just multiply 5 by 3 time. 

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> Power of a Number </title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
 label{
    display: table-cell;
    text-align: justify;
}
input {
  display: table-cell;
}
div.row{
    display:table-row;
}
</style>
<?php
error_reporting(0);

$base = $_POST['base'];
$exponent = $_POST['exponent'];


$base2 = (integer)$base;
$exponent2   = (integer)$exponent;



  $value=1;
  while ($exponent2!=0)
  {
      $value*=$base2;  
      --$exponent2;
  }


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

$title .= "<br> ===== GENERATED RESULT ===== " ."<BR><BR>";
$title .= "The compute result is ".$value.". <br><br>"; 

    
       
}
if(isset($_POST['clear'])) {
   $base = " ";
   $exponent = " ";
   $title= " ";
   
}
?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
  <br> <h1 align="center"> Power of a Number</h2>
 <form action="" method="post"><div>
  <div class="row"><label> Enter Base Number    : </label> <input type="text" name="base" 
           value="<?php echo $base; ?>" autofocus required size=10/><br> </div>
<div class="row"> <label> Enter Exponent Number : </label><input type="text" name="exponent" 
           value="<?php echo $exponent; ?>" required size=10/><br> </div><br>
   <input type="submit" name="check" value="Find Power of a Number" 
  title="Click here to find power of a 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