Sunday 16 October 2016

php logical technical programming question, php programming interview question and answers anichandran


Reffered from :  http://www.letsknowit.com/php-programming-questions

PHP Programming Questions

Write a program to print Factorial of any number

Answer

 <?php
 $number = 6;                   /*number to get factorial */
 $fact   = 1;
 for($k=1;$k<=$number;++$k)    
       {
          $fact =  $fact*$k;
       }
 echo "Factorial of $number is ".$fact;
?>

------------------------------------------------------------------------------------------------------------------
Write a program in PHP to print Fibonacci series . 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Answer

<?PHP 
   $first = 0;
   $second = 1;
   echo $first.'&nbsp;,';
   echo $second.'&nbsp;,';
  
  for($limit=0;$limit<10;$limit++)
   {
     $third = $first+$second;
     echo $third.'&nbsp;,';;
     $first = $second;
     $second = $third;
   
   }
?>
----------------------------------------------------------------------------------------------------------------------
Write a program to find whether a number is Armstrong or not

Answer

// If the sum of cubes of individual digits of a number is equal to the number itslef  then it is called Armstrong Number.
<?php
if(isset($_POST['number']) && $_POST['number']!='') 
    {  
      
        $number = $_POST[ 'number' ];      // get the number entered by user
     
        $temp = $number;
        $sum  = 0;
     
        while($temp != 0 )
        {
            $remainder   = $temp % 10; //find reminder
            $sum         = $sum + ( $remainder * $remainder * $remainder );
            $temp        = $temp / 10;

       }
        if( $number == $sum )
        {
            echo "$number is an Armstrong Number";
        }else
        {
            echo "$number is not an Armstrong Number";
        }
    }
?>
------------------------------------------------------------------------------------------------------------------------
Write a program to print Reverse of any number

Answer

<?php
if(isset($_POST['rev2']))
{
         $rev=0;
         $num=$_POST['rev'];
           

          while($num>=1)
                {
                  $re=$num%10;
                  $rev=$rev*10+$re;
                  $num=$num/10;
                 }
}
?>
<html>
  <head>
      <title>Reverse</title>
 </head>
 <body>
    <table>
         <form name="frm" method="post">
            <tr><td>Number</td><td><input type="text" name="rev"></td></tr>
             <tr><td>Reverse is:</td><td><input type="text" value="<?php if(isset($_POST['rev2'])){echo $rev;} ?>" name="rev1"></td></tr>
             <tr><td> </td><td><input type="Submit" value="Reverse" name="rev2"></td></tr>
        </form>
    </table>
 </body>
</html>
-----------------------------------------------------------------------------------------------------------------------
To check whether a number is Prime or not.


 Answer

<?php
if(isset($_POST['submit']) && $_POST['submit']=='Check' )
{ 
   $check=0;
   $num=$_POST['number'];
   for($i=2;$i<=($num/2);$i++)
     { 
       if($num%$i==0)
         {
          $check++;
          if($check==1)
          {
             break;
          }
         }
     }
}
?>
 <html>
   <head>
     <title>Prime Number</title>
   </head>
 <body>
  <table>
    <form name="frm" method="post" action="">
         <tr><td>Number:</td><td><input type="text" name="number" /></td></tr>
         <tr><td></td><td><input type="submit" name="submit" value="Check" /></td>
         <td>
          <center><span>
           <?php if(isset($_POST['sub']))
           {if($check==0)
               {echo "It is a Prime Number";
               }
           else
                {
                 echo "It is not a Prime Number";}
                }
           ?>
          </span>
          </center>
         </td>
         </tr>
    </form>
  </table>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------
Write a program to print Fibonacci series

 Answer

<?php 
if(isset($_POST['sub']))
{ $fibo=array(0,1);
$num=$_POST['nm1'];
     for($i=2;$i<=$num-1;$i++)
{
       $fibo[$i]=$fibo[$i-1]+$fibo[$i-2];
      }
}
?>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number of Terms:</td><td><input type="text" name="nm1" /></td></tr>
<tr><td></td><td><input type="submit" name="sub" /></td>
<td><center><span>
<?php if(isset($_POST['sub'])){
for($i=0;$i<=$num-1;$i++){echo $fibo[$i].",";}
}
?>  
</span></center>
</td></tr>
</form>
</table>
 </body>
</html>-
----------------------------------------------------------------------------

Write a program to find HCF of two numbers

 Answer

 <?php 
if(isset($_POST['submit']))
 $num1=$_POST['number1'];
 $num2=$_POST['number2'];
         function hcf($i1,$i2)
             {
                 if($i2!=0)
                   {
                     return hcf($i2,$i1%$i2);
                   }
                  else
                   {
                    return $i1;
                   }
             }
             
 $hcfofnumber=hcf($num1,$num2);   
}
?>
<html>
<head>
<title>Hcf</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number1:</td><td><input type="text" name="number1" /></td></tr>
<tr><td>Number2:</td><td><input type="text" name="number2" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit" /></td>
<td><center><span>
<?php if(isset($_POST['submit']))
{
echo "HCF is ".$hcfofnumber; }
?>  
</span></center></td></tr>
</form>
</table>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------
Program to find whether a year is LEAP year or not

 Answer

<?php
if(isset($_POST['submit']))
 {
    $year=$_POST['year'];
    if($year%4==0)
          {
            $leap="It is a leap year";
          }
      else
         {
           $leap="It is not a leap year";
        }
}
 ?>
 <html>
 <head>
 <title>Leap Year</title>
 </head>
 <body>
 <table>
 <form name="frm" method="post" action="">
    <tr><td>Enter the year:</td><td><input type="text" name="year" /></td></tr>
    <tr><td></td>        <td><input type="submit" name="submit" value="submit" /></td>
    <td><center><span> 
     <?php
                     if(isset($_POST['submit'])){   
                        echo $leap;      }  
     
     ?>   
           </span></center></td></tr>
 </form>
 </table>
</body>
</html>
 -------------------------------------------------------------------------------------------------------------------------

Program to print the below format
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*


Answer

<?php
 if(isset($_POST['sub']))
 {
   $rows=$_POST['row'];
   for($i=$rows;$i>=1;--$i)
     {
         for($space=0;$space<$rows-$i;++$space)
            echo "  ";
         for($j=$i;$j<=2*$i-1;++$j)
           echo "* ";
         for($j=0;$j<$i-1;++$j)
             echo "* ";
         echo "<br />";
     }
 }
 ?>
 
<table>
 <form method="post" name="frm" action="">
 <tr>    <td>Enter Number of rows:</td>    <td><input type="text" name="row" /></td> </tr>
 <tr><td></td>    <td><input type="submit" name="sub" /></td> </tr>
 </form>
 </table>
 -----------------------------------------------------------------------------------------------------------------------
Program to print below format.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1



 Answer


<?php
 if(isset($_POST['sub']))
 {
   $rows=$_POST['row'];
   for($i=$rows;$i>=1;--$i)
     {
         for($j=1;$j<=$i;++$j)
         {
            echo $j;
         }
     echo "<br />";
     }
 }
 ?>
 <table>
 <form method="post" name="frm" action="">
 <tr>    <td>Enter Number of rows:</td>    <td><input type="text" name="row" /></td> </tr>
 <tr><td></td>      <td><input type="submit" name="sub" /></td> </tr>
 </form>
 </table>

-------------------------------------------------------------------------------------------------------------------------
Program to print below format
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *

 Answer

<?php
 if(isset($_POST['sub']))
 {
   $rows=$_POST['row'];
      for($i=1;$i<=$rows;$i++)
        {
 
           for($j=1;$j<=$i;$j++)
              {
                echo "*";
              }
            echo "<br />";
        }
 }
 ?>
<table>
 <form method="post" name="frm" action="">
 <tr>    <td>Enter Number of rows:</td>    <td><input type="text" name="row" /></td> </tr>
 <tr><td></td>     <td><input type="submit" name="sub" /></td>  </tr>
</form>
</table>
 -------------------------------------------------------------------------------- 


Write a program to print below format
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8


 Answer


<?php
if(isset($_POST['sub']))
 {
   $row=$_POST['row'];
      for($i=1;$i<=$row;$i++)
        {
 
           for($j=1;$j<=$i;$j++)
              {
                echo $j;
              }
            echo "<br />";
        }
 }
 ?>
 <table>
 <form method="post" name="frm" action="">
 <tr>     <td>Enter Number of rows:</td>     <td><input type="text" name="row" /></td>   </tr>
 <tr><td></td>      <td><input type="submit" name="sub" /></td>  </tr>
 </form>
 </table>
---------------------------------------------------------------------------------------------------------------------------
Write a program to find factor of any number
 Answer

<?php
if(isset($_POST['sub']))
 {     $j=0;
   $factor=array(); 
   $num=$_POST['nm1'];
   for($i=1;$i<=$num;$i++)  
       {
          if($num%$i==0)
            { 
             $j++;
             $factor[$j]=$i;
            }
       }
}
 ?>
 
 <table>
 <form name="frm" method="post" action="">
 <tr> <td>Number:</td> <td><input type="text" name="nm1" /></td> </tr>
 <tr><td></td><td><input type="submit" name="sub" /></td>
 <td><center><span>  
  <?php   
    if(isset($_POST['sub']))  
    { 
       echo "Factors are :";for($i=1;$i<=count($factor);$i++) 
       {          echo $factor[$i].",";

        }
     }       
  ?>   
   </span></center></td></tr>
 </form>
 </table>
----------------------------------------------------------------------------------------------------------------------
Write a program to find table of a number
 Answer

<?php
   if(isset($_POST['sub']))
      {
       $num=$_POST['num'];
       echo "<h1><center>Table of " .$num."</center></h1>";
           for($i=1;$i<=10;$i++)
             {
              echo $num*$i;
              echo "<br />";
             }
       
      }

 ?>

<table>
<form name="frm" method="post">
<tr><td>Number</td><td><input type="text" name="num"></td></tr>
<tr><td> </td><td><input type="Submit" value="Submit" name="sub"></td></tr>
</form>
</table>
  ----------------------------------------------------------------------------------------------------------------------

Write a Program for finding the biggest number in an array without using any array functions.
 Answer

<?php
$numbers = array(12,23,45,20,5,6,34,17,9,56,999);
$length      = count($numbers);
$max         = $numbers[0];
for($i=1;$i<$length;$i++)
  {
      if($numbers[$i]>$max)
        {
          $max=$numbers[$i];
        }
  }
echo "The biggest number is ".$max;
?>
-------------------------------------------------------------------------------------------------------------------------
Write a Program to swap two numbers in PHP.
 Answer

<?php
$a=15;
$b=10;
echo "The value of a is ".$a." and b is ".$b;
echo " before swapping.<br />";
$temp=$a;
$a=$b;
$b=$temp;
echo "The value of a is ".$a." and b is ".$b;
echo " After swapping <br />";
?>
--------------------------------------------------------------------------------------------------------

Write a Program for Bubble sorting in PHP
Hide Answer

<?php
$numbers=array(12,23,45,20,5,6,34,17,9,56);
$n=count($numbers);
 for ($c = 0 ; $c < ( $n - 1 ); $c++)
  {
    for ($d = 0 ; $d < ($n - $c - 1); $d++)
    {
      if ($numbers[$d] > $numbers[$d+1]) /* For decreasing order use < */
      {
        $swap = $numbers[$d];
        $numbers[$d]   = $numbers[$d+1];
        $numbers[$d+1] = $swap;
      }
    }
  }

echo "Sorted list in ascending order <br />";

  for ( $c = 0 ; $c < $n ; $c++ )
     echo $numbers[$c]." ";

?>

-------------------------------------------------------------------------------------------------------------------------

Write a Program for finding the smallest number in an array
 Answer

<?php
$numbers=array(12,23,45,20,5,6,34,17,9,56);
$length=count($numbers);
$min=$numbers[0];
for($i=1;$i<$length;$i++)
  {
      if($numbers[$i]<$min)
        {
          $min=$numbers[$i];
        }
  }
echo "The smallest number is ".$min;

?>

------------------------------------------------------------------------------------------------------------------------

Write a program to print the below format :
1 5 9
2 6 10
3 7 11
4 8 12

 Answer

<?php
 

for($i=1;$i<=4;$i++)
{
 $i1=$i+4;
 $i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}


?>

-------------------------------------------------------------------------------------------------------------------

Write a program for this Pattern:
*****
*      *
*      *
*      *
*****

 Answer

<?php
for($i = 1; $i<=5; $i++){
            for($j = 1; $j<=5; $j++){
               if($i == 1 || $i == 5){
                   echo "*";
               }
               else if($j == 1 || $j == 5){
                   echo "*";
               }
               else {
                   echo "&nbsp;&nbsp;";
               }
             
            }
            echo "<br/>";
}
?>

-------------------------------------------------------------------------------------------------------------------

How to print this Pattern:
*0
***00
******000
**********0000
***************00000
 Answer

 <?php
$k = 0;
for ($i=1; $i<=5; $i++)
{
    $k += $i;
    for ($j=1; $j<=$k; $j++)
    {
       echo "*";
    }
        for ($z=0; $z<$i; $z++)
            {
                echo "0";
            }    
    echo "</br>";  
}
?>

----------------------------------------------------------------------------------------------------------------------

How to write a Floyd's Triangle?
1
23
456
78910
1112131415

 Answer

 <?php
$a = 1;
for($i = 1; $i<=5; $i++)
{
    for($j = 1; $j<=$i; $j++)
    {
        echo $a;
        $a++;
    }
    echo '<br/>';
}
?>

---------------------------------------------------------------------------------------------------------------------

Write a program to make a chess:


 Answer

 <table width="270px" cellspacing="0px" cellpadding="0px" border="1px">  
   <!-- cell 270px wide (8 columns x 60px) -->  
      <?php  
      for($row=1;$row<=8;$row++)  
      {  
          echo "<tr>";  
          for($col=1;$col<=8;$col++)  
          {  
          $total=$row+$col;  
          if($total%2==0)  
          {  
          echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";  
          }  
          else  
          {  
          echo "<td height=30px width=30px bgcolor=#000000></td>";  
          }  
          }  
          echo "</tr>";  
    }  
          ?>  
  </table>  

---------------------------------------------------------------------------------------------------------------------

how to print following table as Output:


Answer

 <table align="left" border="1" cellpadding="3" cellspacing="0">  
    <?php  
    for($i=1;$i<=6;$i++)  
    {  
    echo "<tr>";  
    for ($j=1;$j<=5;$j++)  
      {  
      echo "<td>$i * $j = ".$i*$j."</td>";  
      }  
      echo "</tr>";  
      }  
    ?>  
    </table>

-----------------------------------------------------------------------------------------------------------------------------

How to print following number table:



 <table width="270px" cellspacing="0px" cellpadding="0px" border="1px">  
   <!-- cell 270px wide (8 columns x 60px) -->  
    <?php  
        //$a = 1;
      for($row=1;$row<=10;$row++)  
      {  
          echo "<tr>";
         
          for($col=1;$col<=10;$col++)  
          {  
              echo "<td>" .($col * $row). "</td>";
          }
          //$a++;
          echo "</tr>";
      }
      ?>
 </table>

------------------------------------------------------------------------------------------------------------------------

What would be the output of following:
$a = '1';
echo $b = &$a;
echo $c = "2$b";

 Answer


 OUTPUT:
1   // $b value will be 1
21  // $c value will be 21 as 2 is concatenated to the value of $b.

----------------------------------------------------------------------------------------------------------------------

What would be the output of the following?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);

 Answer


 OUTPUT:
bool(false)   //  As number precede by 0 leads to an octal number.
bool(true)    // as 0123 is enclosed in quotes so it would be concerted to it's numeric value which is 123 so it is true.
bool(false)  // it's false as o123 is quoted and is string so they are not identical.Their data type is different.

--------------------------------------------------------------------------------------------------------------------

What will be the output of the following:
$x = ture and false;
var_dump($x);


 Answer

OUTPUT:
bool(true)   //  and operator works as OR because = operator has the precedence over and.
What would be the output of following:
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);

print_r($array);

 Answer

 OUTPUT:
Array ( [1] => d ) 

Here all keys will be type casted to number 1. but the value the array will hold will be of the data type whose precedence is higher.precedence hirarchy in increment order is as follows:
String("1")  => Number(1) => Float(1.5) => bool(true).  so if we omit true element from array then result will be :
Array ( [1] => c ) 
and so like that.....

---------------------------------------------------------------------------------------------------------------------

Write a program to concatenate two strings character by character. e.g : JOHN + SMITH = JSOMHINTH

 Answer

  <?php
 $str= "JOHN";
$str2 = "SMITH";
$a = str_split($str);
$a2 = str_split($str2);
static $j = 0;
  for($i = 0; $i<= 9; $i++){
   if($i%2 !== 0 && $i >0) {
    array_splice($a,$i,0,$a2[$j]);
   $j++;
    }
  }
 echo $str_new = implode('',$a);
 echo '<br/>';
?>

-----------------------------------------------------------------------------------------------------------------------

Program to find the LCM of two numbers.

 Answer

<?php
if(isset($_POST['submit']))
{
    $num1=$_POST['number1'];
    $num2=$_POST['number2'];
    $hcf = gcd($num1, $num2);
    $lcm = ($num1*$num2)/$hcf;
}
 function gcd($x, $y)
  {
if ($x == 0) {
return $y;
}
while ($y != 0) {
if ($x > $y) {
  $x = $x - $y;
}
else {
  $y = $y - $x;
}
}
    return $x;
  }

?>
<html>
  <head>
   <title>LCM</title>
  </head>
  <body>
      <table>
                <form name="frm" method="post" action="">
                <tr><td>Number1:</td><td><input type="text" name="number1" /></td></tr>
                <tr><td>Number2:</td><td><input type="text" name="number2" /></td></tr>
                <tr><td></td><td><input type="submit" name="submit" value="submit" /></td>
                <td><center><span>
                  <?php
                   if(isset($_POST['submit']))
                  {
                  echo "LCM is ".$lcm;
                  }
               ?>  
             </span></center></td></tr>
               </form>
       </table>
  </body>
</html>