0

Hi Everyone,
How are you all?
Friends i m new on daniweb, and i just want to know that how can i make diamond with stars (*) using "For" Loop in PHP.

My Teacher Gives me an assignment to make like this...

for ($a=1; $a<=5; $a++)
{
	
	for($b=1; $b<=5; $b++)
	{
		echo "*";
	
	}
	echo "<br>";
}

up code is showing me this result

*****
*****
*****
*****
*****

i want like this one below...!

*****
****
***
**
*


Any one here to help me out of this...?
i m waiting for your response...!
Thanks
Best Regards!
MK

Edited by monst3rworld: n/a

5
Contributors
15
Replies
44
Views
7 Years
Discussion Span
Last Post by Krishna_23
Featured Replies
0

Hi Everyone,
How are you all?
Friends i m new on daniweb, and i just want to know that how can i make diamond with stars (*) using "For" Loop in PHP.

My Teacher Gives me an assignment to make like this...

for ($a=1; $a<=5; $a++)
{
	
	for($b=1; $b<=5; $b++)
	{
		echo "*";
	
	}
	echo "<br>";
}

up code is showing me this result

*****
*****
*****
*****
*****

i want like this one below...!

*****
****
***
**
*


Any one here to help me out of this...?
i m waiting for your response...!
Thanks
Best Regards!
MK

Replace inner loop with this..

for($b=1; $b<=6-$a; $b++)

That should do the trick..
cheers!!

0

venkat0904,
While that is an acceptable way to acomplish it,
It is my belief that str_repeat would be the faster of the two, with the less code of the two. Just a thought incase speed is an issue later, aswell.

0

venkat0904,
While that is an acceptable way to acomplish it,
It is my belief that str_repeat would be the faster of the two, with the less code of the two. Just a thought incase speed is an issue later, aswell.

nicko170, i agree with you but as u can see from the kind of problem assigned, it is more likely that teacher provided this as an exercise to get familiar with logic building with for loops. Its not a php specific problem. My teachers used to assign these kind of problems in C/C++.. rather much tougher ones...
your solution is much appreciable though...

0

venkat0904,
Understandable, thankyou for your input. You are probably right with that, either was is an acceptable solution, you'rs being more loop orientated.
Cheers,
Nick

0

venkat0904,
Understandable, thankyou for your input. You are probably right with that, either was is an acceptable solution, you'rs being more loop orientated.
Cheers,
Nick

sure
though I, personally, would have used ur kind of solution if my teacher provide me with such assignments.. :P (provided he/she is not bothered about source code)
Cheers!!

0

Fair enough :P
Where as my teachers liked cleaner/efficient code.
That was a pain in the arse when one had to work with 5 - 6 level deep arrays.
Cheers.

0

Fair enough :P
Where as my teachers liked cleaner/efficient code.
That was a pain in the arse when one had to work with 5 - 6 level deep arrays.
Cheers.

lolzzz..
but i believe code efficiency comes into play after u get ur basics straight...
i still remember my teachers giving me problems like "Do .... without using .... "
pretty annoying at first but realized later that it ws for our own good..
Cheers!!

Edited by venkat0904: n/a

0

Got your PM too, but the question was already nicely answered.

Looks like every1 is concernd abt d problem except d 1 who posted it.. :D

-1
`$a=6; $b=0; $c=0; $space = 1;
  $space = $a - 1;
  for ($c = 1; $c <= $a; $c++)
  {
    for ($b = 1; $b <= $space; $b++)
    echo "       ";

     for ($b = 1; $b <= 2*$c-1; $b++)
     printf("<font size='6' color='red'> * </font> ");
     printf("<br>");
       $space=$space-1;
  }
  $space = 1;
  for ($c = 1; $c <= $a - 1; $c++)
  {
    for ($b = 1; $b <= $space; $b++){
     echo "      ";
     }
    $space++;
    for ($b = 1 ; $b <= 2*($a-$c)-1; $b++) {
      printf("<font size='6' color='red'> * </font> ");
   }
     echo "<br>";
  }`
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.