Logical Programs in PHP Interview Questions

In this article, we’ll go through some logical programs interview questions relating to PHP programming. Most interviewers will ask you to write little PHP logical programs, therefore I’ve included a few examples. If you have any new questions, please post them in the comments area.

Write a Logical Programs for Following Patterns ?

A) Make the following pattern using PHP code in which there are 5 rows and 5 columns. In the first row, there will be one column filled and in the second there will be two, and so on.

Logical Programs pattern1

Code:

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

Output:

Logical Programs pattern1 ans

B) Make the following pattern using PHP code in which there are 5 rows and 5 columns. In the first row, there will be five-column filled and in the second there will be four, and so on. It is the same as the above pattern but in decreasing order.

Logical Programs pattern2

Code:

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

Output:

Logical Programs pattern2 ans

C) Make the following pattern using PHP code in which there are 11 rows and 6 columns. In the first row, there will be one column filled and in the second there will be two, and so on until the sixth row after that, there will be decreasing order. it means this pattern is a combination of both A and B patterns.

Logical Programs pattern3

Code:

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

Output:

Logical Programs pattern3 ans

How to find a factorial of a number ?

The product of an integer and all the integers below it up to one is called a factorial. For example.3! = 3  *2 * 1 = 6.

Code:

<?php
              
      $num = 3;
      $factorial = 1;
      if($num != 0) {
        for($i = $num; $i>=1; $i--) {
          $factorial = $factorial * $i;
        }
      }
      else {
        $factorial = 1;
      }
      echo "Factorial of $num is $factorial";

    ?> 

Output:

Logical Programs factorial

Logical Programs for Prime Numbers

A prime number is a number that can only be divided by one and itself. It signifies that no other number can be divided by the prime number.

The current number is initially saved in the variable $no. Then we’ll determine the reminder for each number by comparing it to itself and the integers below it. If a reminder equals 0, we’ll increase a number named $divisible_count.

We verify the value of $divisible_count after all the divisions; if it is less than 3, the number is a prime number since it is only divisible by 1 and itself.

Code:

<?php
              
              $no = 2;
              $total_display = 0;
              while ($total_display < 100 ) {
                $divisible_count = 0;
                for($i = 1; $i <=$no; $i++) {
                    if($no % $i == 0) {
                      $divisible_count += 1;
                    }
                }
                if($divisible_count < 3) {
                  echo $no . " ";
                  $total_display += 1;
                }
                $no += 1;
              }
?> 

Output:

Logical Programs prime numbers

Check whether a year is leap year or not

Code:

<?php
    $year = 2022;
    if($year % 4 == 0) {
      echo $year." is a leap year";
    }
    else {
      echo $year." is a not leap year";
    }
  ?> 

Output:

leap year

Even and Odd Numbers

Code:

<?php
      $check1 = 3534656;
      $check2 = 3254577;
      if($check1%2 == 0) {
        echo "$check1 is Even number <br/>";
        
      } else {
        echo "$check1 is Odd number <br/>";
      }

      if($check2%2 == 0) {
        echo "$check2 is Even number";
      } else {
        echo "$check2 is Odd number";
      }
    ?> 

Output:

even and odd

Sum of Digits

Code:

<?php
    
    $num = 3434654567;
    $rem = 0;
    $sum = 0;
    for($i = 0; $i <= strlen($num); $i++) {
      $rem = $num % 10;
      $sum = $sum + $rem;
      $num = $num/10;
    }
    echo "Sum of digits is $sum";
    
  ?> 

Output:

sum of digits

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. PHP Round() not working properly [Solved] – round() Function

Leave a Comment