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