PHP program to print the number pattern


To print the number pattern in PHP, the code is as follows −

Example

 Live Demo

<?php
function num_pattern($val)
{
   $num = 1;
   for ($m = 0; $m < $val; $m++)
   {
      for ($n = 0; $n <= $m; $n++ )
      {
         echo $num." ";
      }
      $num = $num + 1;
      echo "
";    } } $val = 6; num_pattern($val); ?>

Output

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

This is similar to generating a star pattern, the only difference being, numbers are generated instead of stars. The function ‘num_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

Updated on: 02-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements