PHP

PHP Programming Logical Interview questions and answers

Pinterest LinkedIn Tumblr

Q1) Write a program for a pattern ?

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

Answer

<?php
for($i=0;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "*&nbsp;";
}
echo "<br>";
}
?>

Q2) Write a program for this pattern?

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

Answer

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

Q3) Write a program for this pattern?

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
*  *  *  *  *  *
*  *  *  *  *
*  *  *  *
*  *  *
*  *
*
<?php
	for($i=0;$i<=6;$i++){
		for($k=6;$k>=$i;$k--){
		echo " &nbsp;";
	}
	for($j=1;$j<=$i;$j++){
		echo "* &nbsp;";
		}
		echo "<br>";
	}
	for($i=5;$i>=1;$i--){
		for($k=6;$k>=$i;$k--){
		echo " &nbsp;";
	}
	for($j=1;$j<=$i;$j++){
		echo "* &nbsp;";
		}
		echo "<br>";
	}
?>

Q4) Write a program for this pattern?

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

Anser

<?php
for ($a=1; $a<=5; $a++)
{
	for($b=1; $b<=5; $b++)
	{
		echo "*";
	}
	echo "<br>";
}
?>

Q5) How to find a factorial of a number ?

Factorial of 5 using for loop is shown below.

Example:

<?php  

$num = 5;  
$factorial = 1;  
for ($x=$num; $x>=1; $x--)   
{  
  $factorial = $factorial * $x;  
}  
echo "Factorial of $num is $factorial";  

?> 

Write A Comment

Open chat
Hey Visitor,

How can I help you?