PHPGurukul

PHP Programming/ Logical Interview questions and answers

Q 1. Write a program for this 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 ?
*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
*  *  *  *  *  *
*  *  *  *  *
*  *  *  *
*  *  *
*  *
*
Answres-

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

<?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 ?
Answer- 

<?php
if(isset($_POST['submit']))
{
 $number = $_POST['num'];     /*number to get factorial */
 $fact   = 1;
 for($k=1;$k<=$number;++$k)
 {
 $fact =  $fact*$k;
 }
 echo "Factorial of $number is ".$fact;
}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 <title>Factorial of any number</title>
 </head>
 <body>
 <form name="factorial" action="" method="post">
 Number :<input type="text" name="num" value="" required=""><br>
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q 6- How to find whether a number prime or not ?
Answer-

<?php
if(isset($_POST['submit']))
{
$check=0;
$num=$_POST['num'];
for($i=2;$i<=($num/2);$i++)
{
if($num%$i==0)
{
$check++;
if($check==1)
{
 break ;
}
}
}
if($check==0)
{
echo "It is a Prime Number";
}
else
{
echo "It is not a Prime Number";
}
}
?>
<!DOCTYPE html>
 <html>
 <head>
 <title>Check whether a number prime or not</title>
 </head>
 <body>
 <form name="primenumber" action="" method="post">
 Number :<input type="text" name="num" value="" required><br>
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q 7- How to find whether a number armstrong or not ?
Answer- An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

<?php
if(isset($_POST['submit']))
{
$number = $_POST['num'];      // 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";
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Whether a number Armstrong or not</title>
</head>
<body>
 <form name="armstrong" action="" method="post">
 Number :<input type="text" name="num" value="" required=""><br>
 <input type="submit" value="Submit" name="submit">
 </form>
</body>
</html>

Q 8- How to find  whether a year leap year or not ?
Answer-

<?php
if(isset($_POST['submit']))
{
$year=$_POST['year'];
if($year%4==0)
{
echo "It is a leap year";
}
else
 {
   echo "It is not a leap year";
 }
}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 <title>Leap year</title>
 </head>
 <body>
 <form name="leapyear" action="" method="post">
 Year :<input type="text" name="year" value="" required=""><br /><br />
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q9 -Write a program for this pattern(number pyramid) ?
1
22
333
4444
55555
Answer-

<html>
<head>
<title>Number Pyramid</title>
</head>
<body>
<?php
$r;
$c;
for($r=1;$r<=5;$r++)
{
for($c=1;$c<=$r;$c++)
{
print("$r");
}
print "<br>";
}
?>
</body>
</html>

Q 10- How to print a number reverse ?
Answer –

<?php
if(isset($_POST['submit']))
{
         $rev=0;
         $num=$_POST['num'];
          while($num>=1)
                {
                  $re=$num%10;
                  $rev=$rev*10+$re;
                  $num=$num/10;
                 }
                   echo "Reverse number of is " .$rev;
}
?>
<!DOCTYPE html>
 <html>
 <head>
 <title>Reverse of a number</title>
 </head>
 <body>
 <form name="reversenumber" action="" method="post">
 Number :<input type="text" name="num" value="" required=""><br>
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q11 – Swap two values without third variable ?
Answer –

<?php
if(isset($_POST['submit']))
{
$value1=$_POST['num1'];
$value2=$_POST['num2'];
$value1=$value1+$value2;
$value2=$value1-$value2;
$value1=$value1-$value2;
        echo "Value of first variable after swapping" .$value1."<br />";
        echo "Value of second variable after swapping"  .$value2;
}
?>
<!DOCTYPE html>
 <html>
 <head>
 <title>Swap two values without third Variable</title>
 </head>
 <body>
 <form name="factorial" action="" method="post">
 Number 1 :<input type="text" name="num1" value="" required=""><br /><br />
 Number 2 :<input type="text" name="num2" value="" required=""><br />
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


10 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.