Star Patterns and Basic Logic Questions in PHP

Saurabh DamaleSaurabh Damale
5 min read

Welcome to a special blog post where we delve into some interesting PHP challenges: star patterns and basic logic questions. These exercises are great for honing your programming skills and understanding loops, and conditionals in PHP.

Star Patterns

Star patterns are a common exercise in programming that help you understand nested loops. Here are some examples:

  1. Right-Angled Triangle:

     <?php
         $n = 5; // Number of rows
         for ($i = 1; $i <= $n; $i++) {
             for ($j = 1; $j <= $i; $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Outputs:
         // *
         // * *
         // * * *
         // * * * *
         // * * * * *
     ?>
    
  2. Inverted Right-Angled Triangle:

     <?php
         $n = 5;
         for ($i = $n; $i >= 1; $i--) {
             for ($j = 1; $j <= $i; $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Outputs:
         // * * * * *
         // * * * *
         // * * *
         // * *
         // *
     ?>
    
  3. Left-Angled Triangle:

     <?php
         $n = 5;
         for ($i = 1; $i <= $n; $i++) {
             for ($j = $i; $j < $n; $j++) {
                 echo "&nbsp;&nbsp;";
             }
             for ($j = 1; $j <= $i; $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Output:
         //         *
         //       * *
         //     * * *
         //   * * * *
         // * * * * *
     ?>
    
  4. Inverted Left-Angled Triangle:

     <?php
         $n = 5;
         for ($i = $n; $i >= 1; $i--) {
             for ($j = $i; $j < $n; $j++) {
                 echo "&nbsp;&nbsp;";
             }
             for ($j = 1; $j <= $i; $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Output:
         // * * * * *
         //   * * * *
         //     * * *
         //       * *
         //         *
     ?>
    
  5. Pyramid:

     <?php
         $n = 5;
         for ($i = 1; $i <= $n; $i++) {
             for ($j = $i; $j < $n; $j++) {
                 echo "&nbsp;&nbsp;"; // Two spaces
             }
             for ($j = 1; $j <= (2 * $i - 1); $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Outputs:
         //         *
         //       * * *
         //     * * * * *
         //   * * * * * * *
         // * * * * * * * * *
     ?>
    
  6. Diamond:

     <?php
         $n = 5;
         for ($i = 1; $i <= $n; $i++) {
             for ($j = $i; $j < $n; $j++) {
                 echo "&nbsp;&nbsp;";
             }
             for ($j = 1; $j <= (2 * $i - 1); $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
         for ($i = $n-1; $i >= 1; $i--) {
             for ($j = $n; $j > $i; $j--) {
                 echo "&nbsp;&nbsp;";
             }
             for ($j = 1; $j <= (2 * $i - 1); $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Outputs:
         //         *
         //       * * *
         //     * * * * *
         //   * * * * * * *
         // * * * * * * * * *
         //   * * * * * * *
         //     * * * * *
         //       * * *
         //         *
     ?>
    
  7. Sandglass Pattern:

     <?php
         $n = 5;
         // Upper part
         for ($i = $n; $i >= 1; $i--) {
             for ($j = $i; $j < $n; $j++) {
                 echo "&nbsp;&nbsp;";
             }
             for ($j = 1; $j <= (2 * $i - 1); $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
         // Lower part
         for ($i = 2; $i <= $n; $i++) {
             for ($j = $i; $j < $n; $j++) {
                 echo "&nbsp;&nbsp;";
             }
             for ($j = 1; $j <= (2 * $i - 1); $j++) {
                 echo "* ";
             }
             echo "<br>";
         }
    
         // Output:
         // * * * * * * * * *
         //   * * * * * * *
         //     * * * * *
         //       * * *
         //         *
         //       * * *
         //     * * * * *
         //   * * * * * * *
         // * * * * * * * * *
     ?>
    

Basic Logic Questions

  1. Swapping of Two Variables:
<?php
    $a = 5;
    $b = 10;

    // Swapping without using a third variable
    echo "Before swapping: a = $a, b = $b<br>";
    $a = $a + $b; // a becomes 15
    $b = $a - $b; // b becomes 5 (15 - 10)
    $a = $a - $b; // a becomes 10 (15 - 5)
    echo "After swapping: a = $a, b = $b<br>";
?>
  1. Find the Largest Number Among Three Numbers:
<?php
    $num1 = 10;
    $num2 = 20;
    $num3 = 30;

    if ($num1 >= $num2 && $num1 >= $num3) {
        echo "$num1 is the largest number.";
    } elseif ($num2 >= $num1 && $num2 >= $num3) {
        echo "$num2 is the largest number.";
    } else {
        echo "$num3 is the largest number.";
    }
?>
  1. Check if a Number is a Leap Year or Not:
<?php
    $year = 2024;

    if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
        echo "$year is a leap year.";
    } else {
        echo "$year is not a leap year.";
    }
?>
  1. Write a Program to Find the Area of a Circle:
<?php
    $radius = 5;
    $area = 3.14 * ($radius * $radius);

    echo "The area of the circle with radius $radius is " . round($area, 2) . ".";
?>

Conclusion

In this blog post, we covered various star patterns and basic logic questions in PHP. These exercises help solidify your understanding of loops and conditionals, which are fundamental concepts in PHP programming. Try implementing these patterns and logic questions on your own to further enhance your coding skills.

0
Subscribe to my newsletter

Read articles from Saurabh Damale directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Damale
Saurabh Damale

Hello there! ๐Ÿ‘‹ I'm Saurabh Kailas Damale, a passionate Full Stack Developer with a strong background in creating dynamic and responsive web applications. I thrive on turning complex problems into elegant solutions, and I'm always eager to learn new technologies and tools to enhance my development skills.