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

Monday, 10 June 2013

How to convert decimal number to octal number

The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7. Octal widely used in computing when systems such as the PDP-8, ICL 1900 and IBM mainframes employed 12-bit, 24-bit or 36-bit words.

Steps to convert a decimal number to octal number:

1. Take modulas(%) of decimal number by 8.
2. Now divide decimal number by 8.
3. Repeat these steps until condition matches.
4. Now reverse that number.

Go through the code given below.

<?php
     $numberToConvert = 78;
     if($numberToConvert > 0){
           $tempArray = array();
           echo "Octal of $numberToConvert is : ";
           while($numberToConvert){
               $tempArray[] = $numberToConvert%8;
               $numberToConvert = (int)($numberToConvert/8);
           }
           for($i = count($tempArray); $i >=0; $i--){
               echo $tempArray[$i];
           }      
     }else{
           echo 'Number should be greater then zero.';
     }
?>
Output is : Octal of 78 is : 116

Saturday, 8 June 2013

How to convert decimal number to binary

The binary numeral system, or base-2 numeral system, represents numeric values using two symbols: 0 and 1. More specifically, the usual base-2 system is a positional notation with a radix of 2.

Numbers represented in this system are commonly called binary numbers. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers and computer-based devices such as mobile phones.

Steps to convert a decimal number to Binary number:

1. Take modulas(%) of decimal number by 2.
2. Now divide decimal number by 2.
3. Repeat these steps until condition matches.
4. Now reverse that number.

Go through the code given below.

<?php
     $numberToConvert = 7;
     if($numberToConvert > 0){
           $tempArray = array();
           echo "Binary of $numberToConvert is : ";
           while($numberToConvert){
               $tempArray[] = $numberToConvert%2;
               $numberToConvert = (int)($numberToConvert/2);
           }
           for($i = count($tempArray); $i >=0; $i--){
               echo $tempArray[$i];
           }      
     }else{
           echo 'Number should be greater then zero.';
     }
?>
Output is : Binary of 7 is : 111

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

Thursday, 30 May 2013

How to reverse number and add using php

Many time interviewer asked me to write a program to reverse a number and add. So i am sharing this program with you.

Steps to reverse a number and add:

  • Take modulas(%) of number by 10. 
  • Add this number until condition matches. 
  • Now divide number by 10. 
  • Repeat these steps until condition matches.

That's it you got the number reversed and added.

<?php
             echo 'How to reverse number and add using php ';
             echo '<br>';
             echo $numberToReverse = 123456789;
             echo '<br>';
             if($numberToReverse > 0){
                    $sum = 0;
                    while($numberToReverse){
                           echo $numberToReverse%10;
                           $sum += $numberToReverse%10;
                           $numberToReverse = (int)($numberToReverse/10);
                     }
                     echo '<br>';
                     echo 'Sum of number : '.$sum;
              }else{
                     echo 'number should be grater then zero.';
              }
?>

Output is:
How to reverse number and add using php

123456789 // actual number

987654321 // reversed number

Sum of number : 45

How to reverse number using php

Many time interviewer asked me to write a program to reverse a number. So i am sharing this program with you.

Steps to reverse a number:
1. Take modulas(%) of number by 10.
2. Now divide number by 10.
3. Repeat these steps until condition matches.

That's it you got the number reversed.

<?php
             echo 'How to reverse number using php ';
             echo '<br>';
             echo $numberToReverse = 123456789;
             echo '<br>';
             if($numberToReverse > 0){
                     while($numberToReverse){
                               echo $numberToReverse%10;
                                $numberToReverse = (int)($numberToReverse/10);
                     }
              }else{
                     echo 'number should be grater then zero.';
              }
?>
Output is:
How to reverse number using php

123456789 // actual number

987654321 // reversed number

Tuesday, 28 May 2013

How to reverse string without php function

In many interview, interviewer asked me to write program for string reverse without php function, in this post i am shareing this program.

It's logic is very simple, just get substing from right han side and length should be one. 

<?php
            $stringToReverse = 'crunchcodes';
            $length = strlen($stringToReverse);

            for($i = $length; $i >= 0; $i--){
                  echo substr($stringToReverse, $i, 1);
            }
?>

Output is:

Actual string - crunchcodes
Outut string - sedochcnurc

Monday, 27 May 2013

How to implement Star pattern in php

In interview, interviewer asked me to write program for star pattern. it was just to check my logic. in this post I am sharing this program.

Inner loop prints line wise and outer loop prints column wise.

<?php
           for($i = 0; $i < 10 ; $i++){
                 for($j = 0; $j <= $i ; $j++){
                       echo '*';
                 }
                 echo '<br/>';
            }
?>


Output of the above code will be

*
**
***
****
*****
******
*******
********
*********
**********

For reverse star pattern click here 

Tuesday, 21 May 2013

How to implement reverse Star pattern in php

In interview, interviewer asked me to write program for reverse star pattern. it was just to check my logic. in this post I am sharing this program.

Inner loop prints line wise and outer loop prints column wise.

<?php
           for($i = 10; $i > 0 ; $i--){
                  for($j = 0; $j < $i ; $j++){
                         echo '*';
                  }
                  echo '<br/>';
            }
?>

Output of the above code will be
**********
*********
********
*******
******
*****
****
***
**
*

Tuesday, 30 April 2013

How to print Floyd's triangle in php

Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd.

It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

<?php 
           $a = 1;
           for($i = 1; $i <= 4; $i++){
                 for($j = 1; $j <= $i; $j++ ){
                       echo $a;
                       $a++;
                  }
                   echo '<br/>';
           }
?>
Output is:
1
23
456
78910