Here I give you some basic pattern which are asked in interview questions.
Pattern 1:
*
* *
* * *
* * * *
* * * * *
Pattern 2:
1
22
333
4444
55555
Pattern 3:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Pattern 4:
1
12
123
1234
12345
Code:
<?php
echo "Pattern 1: <br>";
function create($n){
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j <= $i; $j++) {
echo " * ";
}
echo "<br>";
}
}
$n = "5";
create($n);
?>
<br><?php
echo "Pattern 2: <br>";
function p($n){
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $i;
}
echo "<br>";
}
}
p('5');
?>
<br>
<?php
echo "Pattern 3: <br>";
function p1($n){
$num = 1;
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j <= $i; $j++) {
echo " ".$num." ";
$num++;
}
echo "<br>";
}
}
$n="6";
p1($n);
?>
<br>
<?php
echo "Pattern 4: <br>";
function p2($n){
$num = 1;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $j;
}
echo "<br>";
}
}
$n = 5;
p2($n);
?>
No comments:
Post a Comment