PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

19 Aug 2015

Find Armstrong Numbers between 1 to 1000 in PHP

            This blog describes how to find Armstrong Numbers between 1 to 1000 using PHP?. Before we are going to program part, we should know what is Armstrong Number?. So only we can write program for find Armstrong Number in any languages like C, C++, PHP.

what is Armstrong Number?


           Armstrong Number is sum of 3 power of each digits equals to given number.
For example: 
           153 is Armstrong Number.
           1^3 + 5^3 + 3 ^3
           1 + 125 + 27
           153

PHP Program to find Armstrong Numbers:


          Consider below example which is find Armstrong Numbers between 1 to 1000.

<?php
for($i = 1; $i <= 1000; $i++) {
  $sum = 0;
  $qu = $i;
  while($qu != 0) {
$remainder = $qu % 10;
$thrice = $remainder * $remainder * $remainder;
$sum += $thrice;
$qu = $qu / 10;
  }
  if ($sum == $i) {
    echo "<p>$i is armstrong number</p>";
  }
}
?>


PHP program for find Armstrong Numbers


              Now you got Armstrong Numbers between 1 to 1000.

Related Post:

13 Aug 2015

Get value from drop down box using PHP

                        The drop down box allows you to select one or more values from it. The HTML tag "select" is also known as "drop down" or "pul down" box.You can get value from drop down box using PHP while form submitting.

HTML form with drop down box


                        The below example describes how to create HTML form with drop down box.

<html>
<body>
<form action="#" method="post">
  <select name="country">
     <option value="USA">USA</option>
     <option value="United Kingdom">United Kingdom</option>
     <option value="France">France</option>
     <option value="Russia">Russia</option>
     <option value="India">India</option>
  </select>
  <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>

Retrieve input data from drop down box using PHP


                       You can retrieve input data from drop down box using following types in PHP.
             $_POST['drop_down_name'] (if method is post)
             $_GET['drop_down_name'] (if method is gett)
             $_REQUEST['drop_down_name'] (if method is either post or get)
                         The following PHP script describes how to retrieve input data from drop down box while form submitting.

<html>
<body>
<form action="#" method="post">
   <select name="country">
      <option value="USA">USA</option>
      <option value="United Kingdom">United Kingdom</option>
      <option value="France">France</option>
      <option value="Russia">Russia</option>
      <option value="India">India</option>
   </select>
   <input type="submit" name="submit" value="Submit">
</form>
<?php
      if(isset($_POST['submit']))
      {
         $country=$_POST['country'];
         echo'You selected '.$country;
       }
?>
</body>
</html>

                             While submitting form with select country from drop down box, you can get selected country as output.

Related Post:

17 Jun 2015

Fibonacci series of given number in PHP

         This blog describes about how to find Fibonacci series of given number in PHP. Fibonacci series is a sequence of number generated by adding previous two numbers. If the given number is 'n', then Factorial of 'n' is defined as 'Fn'. In mathematical terms, Fibonacci series for 'n' number looks like this:

                                            Fn = Fn-1 + Fn-2;

         Where,
              F0 = 0;
              F1 = 1;

         Fibonacci series for 'n' number is calculated recursively by above formula.

Simple PHP program Fibonacci series:


         This is the simple PHP program for find Fibonacci series.


function fibonacci_series($n) {
  $f1 = -1;
  $f2 = 1;

  for ($i = 1; $i <= $n; $i++) {
    $f = $f1 + $f2;
    $f1 = $f2;
    $f2 = $f;
    echo "$f<br />";
  }
}

echo fibonacci_series(5);


         The above PHP program returns 0, 1, 1, 2, 3. As i said, Fibonacci series is a sequence of number generated by adding previous two numbers. In above program, we have generated Fibonacci series for 5.
We have assumed $f1 = -1; and $f2 = 1; We need to run for loop until 5.As per above program,

If $i = 1:
              $f = -1 + 1 = 0;
              $f1 = 1;
              $f2 = 0;

If $i = 2:
              $f = 1 + 0 = 1;
              $f1 = 0;
              $f2 = 1;

If $i = 3:
              $f = 0 + 1 = 1;
              $f1 = 1;
              $f2 = 1;

If $i = 4:
              $f = 1 + 1 = 2;
              $f1 = 1;
              $f2 = 2;

If $i = 5:
              $f = 1 + 2 = 3;
              $f1 = 2;
              $f2 = 3;

Fibonacci series for 'n' number in PHP:


         The below PHP program is used to find Fibonacci series of given number.


<html>
<head>
<style type="text/css">
 body {
 color:white;
 font-size:14px;
 }
 .contact {
    text-align:center;
    background: none repeat scroll 0% 0% #8FBF73;
    padding: 20px 10px;
    box-shadow: 1px 2px 1px #8FBF73;
    border-radius: 10px;
    width:510px;
 }
 #number {
    width: 250px;
    margin-bottom: 15px;
    background: none repeat scroll 0% 0% #AFCF9C;
    border: 1px solid #91B57C;
    height: 30px;
    color: #808080;
    border-radius: 8px;
    box-shadow: 1px 2px 3px;
    padding: 3px 4px;
}
#submit
{
    background:none repeat scroll 0% 0% #8FCB73;
    display: inline-block;
    padding: 5px 10px;
    line-height: 1.05em;
    box-shadow: 1px 2px 3px #8FCB73;
    border-radius: 8px;
    border: 1px solid #8FCB73;
    text-decoration: none;
    opacity: 0.9;
    cursor: pointer;
    color:white;
}
#er {
    color: #F00;
    text-align: center;
    margin: 10px 0px;
    font-size: 17px;
}
</style>
</head>
<body>
<div class="contact">
  <h1>Fibonacci series in PHP</h1>
  <form action="#" method="POST">
    Enter number : <input type="text" name="number" id="number" /></br>
    <input  type="submit" name="submit" id="submit" value="Submit"></input>
  </form>

  <?php
   if(isset($_POST['submit'])) {
     $n = $_POST['number'];
   
     function fibonacci_series($a) {
       $f1 = -1;
       $f2 = 1;

       for ($i = 1; $i <= $a; $i++) {
        $f = $f1 + $f2;
        $f1 = $f2;
        $f2 = $f;
        echo "$f<br />";
       }
     }
 
    echo "Fibonacci series of $n is:<br />";
    echo fibonacci_series($n);

   }


  ?>
</div>
</body>  
</html>


          When you open above PHP file in browser, it looks like below image:


Find Fibonacci series for Given number in PHP


        If you type number 6 in textbox, it returns 0, 1, 1, 2, 3, 5. It looks like below image:


Simple Fibonacci series program in PHP


        Now you can find Fibonacci series for any number in PHP. I've hope, you got an idea for generate Fibonacci series of given number.