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