Given a positive non-zero integer not greater than the number of characters rows on your VDU, place one "*" character in the centre column, on the top line. Then, for each succeeding line place two additional "stars" centrally below the previous line, to form a pyramid. E.g. for four lines:
*
***
*****
*******
<? // randomly generate an amount // this script can handle any positive number $amount = mt_rand(2, 12); // align text echo '<pre>' . $amount . ':<br/>'; // loop through the number of lines for ($n = 1; $n <= $amount; $n++) { // the amount of stars is double the amount minus 1 $stars = str_repeat('*', ($n*2)-1); // the amount of spaces is the amount minus the current line $spaces = str_repeat(' ', $amount-$n); // echo out the line of the pyramid echo $spaces . $stars . $spaces . '<br/>'; } // return to normal format echo '</pre>'; ?>Which produces:
11:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
A C++ solution. This would be better if I knew what function is used to repeat characters.
#include <stdio.h> int main(int argc, char *argv[]){ int iAmount, a, b; printf("How many rows?\n>"); scanf("%d", &iAmount); for (a = 1; a <= iAmount; a++) { for (b = 0; b < iAmount-a; b++) printf(" "); for (b = 1; b < a*2; b++) printf("*"); printf("\n"); } return 1;}I've converted the code above into a nifty pyramid that's similar to the output ;)
#include <stdio.h>int main(int argc, char *argv[]){ ; int r,o,w ;scanf( "%d",&r); for(w=1;w<= r;w++){for(o= 0;o<(r-w);o+=1) printf(" ");for(o =1;o<w*2;o++)printf ("*");printf("\n");};}