Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Saturday, 3 August 2013

How to make a Fibonacci series using PHP

In this article i will write a program to print Fibonacci numbers upto 20. This is very basic program we were writing in our college time. In Fibonacci number each number is sum of previous tow numbers. first tow numbers of Fibonacci number is 0 and 1, the next number will be (0+1) = 1, next will be (1+1) = 2, next will be (1+2) = 3 and so on.
Here is the code for Fibonacci numbers.

<?php
     $limit = 20;
     echo $first = 0;echo '<br/>';
     $second = 1;
     while($limit > 0){
          echo $next = $first + $second;echo '<br/>';
          $second = $first;
          $first = $next;
          $limit--;
     }
?>
Here is the Output :

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

Thursday, 6 June 2013

check odd or even number in php

Even number can be devided by 2, All Even numbers always end with a digit of 0, 2, 4, 6 or 8. 2, 14, 16, 30 etc.

Odd number can not be devided by 2, All Odd numbers always end with a digit of 1, 3, 5, 7, or 9 2, 8, 16, 30 etc.

<?php
     $number = 13; // input number
     if($number%2 == 0){
          echo "$number is even number";
     }else{
          echo "$number is odd number";
     }
?>
Output is:
13 is odd number

Wednesday, 5 June 2013

How to check vowels in php

A vowel is a speech sound made by the vocal cords. It is also a type of letter in the alphabet.
There are 5 vowel are there. A, E, I, O, U.

<?php 
     $char = 'a';    // input character
     switch ($char){
         case 'a':
         case 'A':
         case 'e':
         case 'E':
         case 'i':
         case 'I':
         case 'o':
         case 'O':
         case 'u':
         case 'U':
       
         echo "character $char is a vowel";
             break;
         default :
             echo "character $char is not a vowel";
}
?>
Output is:
character a is a vowel

Tuesday, 4 June 2013

Generate factorial of a number in php

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

The factorial operation is encountered in many different areas of mathematics, notably in combinatorics, algebra and mathematical analysis.

<?php 
     $a = 1;
     for($i = 4; $i > 0; $i--){
         $a *= $i;
     }
     echo $a;
?> 
Output is: 24