Tos Web Developer provides insights, tutorials, and advice around topics including content strategy, design, Drupal Development, Drupal Custom Module Development, Drupal 8 Custom themes, PHP, Server, Twig, and more



If I pass value Dynamically in this patter like the following patter will be print. You have solved this question in within 15-20 min

Function to print the following structure:

$n = 3


1 _ 3
_ 2 _
1 _ 3


$n = 5


1 _ _ _ 5
_ 2 _ 4 _
_ _ 3 _ _
_ 2 _ 4 _
1 _ _ _ 5



$n = 7


1 _ _ _ _ _ 7
_ 2 _ _ _ 6 _
_ _ 3 _5_ _
_ _ _ 4_ _ _
_ _ 3 _ 5 _ _
_ 2 _ _ _ 6 _
1 _ _ _ _ _ 7

 function print_structure($n)
{

}


Answer :


 <?php  
 $n = 37;  
 function print_structure($n)  
 {  
   for ($i = 1; $i <= $n; $i++) {  
     for ($j = 1; $j <= $n; $j++) {  
       if ($i == $j || $j == ($n + 1) - $i) {  
         echo $i;  
       } else {  
         echo '_ ';  
       }  
     }  
     echo '<br>';  
   }  
 }  
 print_structure($n);  
 ?>   

No comments:

Post a Comment

| Designed And Blog Post by www.toswebdeveloper.com