Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

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, 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