How can I output these with a simple 'for' loop?
####*
###**
##***
#****
*****
*****
#****
##***
###**
####*
hashtag = spaces
I've tried to changing the * and the 'br' within this one:
for($i = 0; $i < 5; $i++){
for($b = 0; $b <= $i; $b++) {
echo '*';
}
echo "</br>";
}
But than it will just output them under each other >.>
for($i=1;$i<6;$i++){
for($k=0;$k<5-$i;$k++){
echo " ";
}
for($j=0;$j<$i;$j++){
echo "*";
}
echo "<br>";
}
echo "<br>";
for($i=0;$i<6;$i++){
for($j=0;$j<$i;$j++){
echo " ";
}
for($k=0;$k<5-$i;$k++){
echo "*";
}
echo "<br>";
}
My Solution. Next time you should do your homework by yourself ;-)
Related
I would like to duplicate the pattern, the function will have 2 parameters, which is row and column. It will duplicate the pattern on how many rows and columns. My function for pattern is correct but how can I duplicate/replicate it. Should I make it in array? Thanks
<?php
$row_col_array = array();
function honey(){
$l = "";
for($i=2; $i<=4; $i++)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
for($i=4; $i>=2; $i--)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
}
honey();
?>
I try to make a function that creates some triangles.
<pre>
<?php
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
for ($j=1;$j<=$max;$j++){
if ($i = $j){
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
echo " ". $ctr * $ctr;
}else{
echo " ";
}
}
echo ""."<br>";
}
}
?>
</pre>
The line of for ($i=1; $i<=$angka; $i++) is stop. It just run it once.
The next $i is not running. There is no next $i.
Actually, I want to make it as a function it, but this still doesn't work.
It looks like this may be the culprit
if ($i = $j){
It should be
if ($i == $j){
Because you're setting $i to $j which if $angka is 1, it will exit the for loop since it's $i <=$angka
Here is the fixed code:
<?php
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
///*
for ($j=1;$j<=$max;$j++){
if ($i == $j){ //NOTICE how this needs to be `==` and not `=`
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
//$segitiga = $segitiga . ($ctr * $ctr);
echo " ". $ctr * $ctr;
}else{
//$segitiga = $segitiga . " ";
echo " ";
}
}
//*/
//$segitiga = $segitiga . "\n";
echo ""."<br>";
}
//echo $segitiga;
}
?>
for ($i=1; $i<=$angka; $i++){
your $i start at 1 and stops where it is equal to $angka
where $angka could be 0 because of
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
so that loop may never execute , not even once
if you want to test what I'm saying, I'm executing your code with $angka set manually and it runs normally
here is the output for the following code
$angka = "3";
echo "<pre>";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
echo 'the i loop here'.PHP_EOL;
for ($j=1;$j<=$max;$j++){
echo 'the j loop here'.PHP_EOL;
if ($i == $j){
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
echo " ". $ctr * $ctr;
}else{
echo " ";
}
}
echo ""."<br>";
}
}
I want to print star pattern like :
below is my code. but it doesn't work for me. How can i fix this problem.
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
?>
You should put all text that should keep form and not whitespace trimmed in between a <pre/> tag
<pre><?php
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
} ?></pre>
Note: if your target is console and PHP-CLI, don't put the <pre/> tag, omit the end of PHP tag (?>) and replace all <br/> tags with \n.
Thanks to all of you guys. I add <pre> tag before code and also change <br> tag with \n and now it is work. Thanks again. Have a nice day guys..
Try this...
function printPattern($n)
{
$k = 0;
// Print upper triangle
for ($i = 1; $i <= $n; $i++)
{
// Print spaces
for ($j = 1; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
$k = 0;
// move to next row
echo "\n";
}
$n--;
// Print lower triangle
for ($i = $n; $i >= 1; $i--)
{
// Print spaces
for ($j = 0; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
$k = 0;
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
echo "\n";
}
}
// Driver Code
$n = 6;
printPattern($n);
// This Code is contributed by mits
?>
Please see my code.
for ($row = $var; $row >= 1; --$row) {
for($j=0;$j<$row;++$j)
{echo "*";}
echo "</br>";
echo " ";
Output:
*****
****
***
**
*
But i need output as below:
*****
****
***
**
*
You might want to check out some of the string functions:
<?php
for ($i = 5; $i > 0; $i--) {
echo str_repeat(' ', 5 - $i).str_repeat('*',$i).PHP_EOL;
}
for ($i = 5; $i > 0; $i--) {
echo str_pad(str_repeat('*',$i),5,' ',STR_PAD_LEFT).PHP_EOL;
}
This runs on the command line, like so:
php filename.php
<?php
$var = 5;
echo "<div style='font-family:Courier New, Courier, monospace;'>";
for ($row = $var; $row >= 1; $row--) {
for($i=$row;$i<$var;$i++)
{
echo " ";
}
for($j=0;$j<$row;$j++)
{
echo "*";
}
echo "<br>";
}
echo "</div>";
?>
This question already has answers here:
How can I create a pyramid from using php?
(14 answers)
Closed 8 years ago.
I am having problem creating a pyramid of asterisk.
Please see my code.
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
Result:
*
**
***
****
*****
My question is how I am going to make that like.
*
* *
* * *
* * * *
* * * * *
<pre><?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?></pre>
use the same program within <center> </center> tag ! like:
<center>
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
</center>
Use HTML whitespace character to procude the whitespaces:
So something like this:
<?php
// pyramid height
$height = 5;
for($i=1;$i<=$height;$i++){
for($t = 1;$t <= $height-$i;$t++)
{
echo " ";
}
for($j=1;$j<=$i;$j++)
{
// use here to procude space after each asterix
echo "* ";
}
echo "<br />";
}
?>
try this
$height = 5;
$space = $height;
for($i = 1; $i <= $height; $i++) {
echo str_repeat(' ', --$space);
for($j=1;$j<=$i;$j++){
if($j > 1) {
echo ' ';
}
echo '*';
}
echo '<br />';
}
create_pyramid("*", 5);
function create_pyramid($string, $level) {
echo "<pre>";
$level = $level * 2;
for($i = 1; $i <= $level; $i ++) {
if (!($i % 2) && $i != 1)
continue;
print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH);
print PHP_EOL;
}
}
From link posted above by Baba
$n = 5;
$i = 0;
for($i=1; $i<=$n; $i++){
echo "<pre>";
echo str_repeat(" ", $n-$i);
echo str_repeat("# ", $i);
}