PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: August 2015

26 Aug 2015

Find prime numbers between 1 and 100 in PHP

        This blog describes how to find prime numbers between 1 and 100 using PHP. Prime number is a number which is divided by 1 and itself.  Consider the number 5. It is prime number because it can be divided by 1 and 5.

PHP program for prime numbers:


       Consider the below program which is used to find prime numbers from 1 to 100.

<?php
    for($i = 1; $i <= 100; $i++)
      $mm = 0;
      for($j = 2; $j <= $i/2; $j++) {
        //only not prime numbers
                if ($i % $j == 0) {
                  $mm++;
                  break;
                }
      }
      if ($mm == 0) {
                echo"$i is prime number<br/>";
      }
    }
  ?>

       In above program, if a number is divided by any number except 1 and itself, then it is not prime number. Otherwise it is a prime number.


PHP program for find prime numbers from a to 100


        Now you got prime numbers between 1 and 100 using PHP.
Related Post

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: