Google+ PHP TUTORIALS ONLINE HELP FOR MEDICAL AND PROGRAMMING: 2014-03-23

Wednesday 26 March 2014

Fibonacci series program in php

Fibonacci series program in php

// Fibonacci series program in php?.
 //In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation.
Example

<html>
<head>
<title>Fibonacci series program in php</title>
</head>
<body>

 <?php
  $count= 0;
 $x = 0;
 $y = 1;
 echo $x."<br/>";
 echo $y."<br/>";
 while($count<10){
 $z = $x + $y;
    echo $z."<br />";
    $x = $y;
    $y = $z;
    $count ++;
 }
?>
</body>
</html>
output:
0
1
1
2
3
5
8
13
21
34
55

89

Monday 24 March 2014

Prime numbers in between a range of 100

Program to find prime numbers in between a range of 100

Prime Number example in PHP. We will print the prime number between a range of 100.The prime number is not divisible by 2 only divisible by 1 and itself.

Example
    <html>
     <head>
      <title>PHP Prime Numbers</title>
         </head>
          <body>
     <?php
       error_reporting(E_ALL);
         //Program to find prime numbers in between a range of 100
        $num =100;
          for( $j = 2; $j <= $num; $j++ )
            {
        for( $k = 2; $k < $j; $k++ )
       {
           if( $j % $k == 0 )
       {
         break;
        }
        }
           if( $k == $j )
          echo "Prime Number : ", $j, "<br>";
          }
        ?>
</body>
</html>

Output
Prime Number : 2
Prime Number : 3
Prime Number : 5
Prime Number : 7
Prime Number : 11
Prime Number : 13
Prime Number : 17
Prime Number : 19
Prime Number : 23
Prime Number : 29
Prime Number : 31
Prime Number : 37
Prime Number : 41
Prime Number : 43
Prime Number : 47
Prime Number : 53
Prime Number : 59
Prime Number : 61
Prime Number : 67
Prime Number : 71
Prime Number : 73
Prime Number : 79
Prime Number : 83
Prime Number : 89
Prime Number : 97

Reverse String, without strrev()

Reverse String, without strrev()

In this example we have discus how to print  Reverse String, without strrev() function .Fist off all we Assign the given string to the variable and Calculate the length of the string. We use Iterate the string using for loop with appropriate looping and count Concatenate the string inside the for loop. you can see below example.

Example

   <html>
   <head>
   <title>Reverse</title>
   </head>
    <body>
    <?php
            $string = 'Hello php';
                 $length = strlen($string);
           for ($i=($length-1) ; $i >= 0 ; $i--) {
       echo $string[$i];
       }
      ?>
</body>
</html>

Output:  php olleH

PHP strrev() Function

We can also print without loop

< html>
<html>
<body>

<?php
     echo strrev("Hello World!");
     ?>

</body>
</html>

How to reverse a number in php without built(strrev()) in functions

In this example First off all we get the given number and extract the final digit. Initially the reverse of the number will be zero, on each iteration multiply the reverse with 10 and add the modulus result.you can see below example.

Example

 <html>
 <head>
 <title>reverse number</title>
 </head>
 <body>
      <?php
               $number = 1234; $x =0; $y =0;
                  $originalnum = $number;
                  while($number > 1) {
            $x = $number %10;
              $y = ($y * 10)+ $x;
             $number = $number /10;
           }
          Echo "Original Number: ". $originalnum;
          Echo " Reverse : ". $y;
          ?>
</body>
</html>

Output: Original Number: 1234 Reverse : 4321