PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: PHP Program
PHP Program - phponwebsites.com
Showing posts with label PHP Program. Show all posts

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: