I want to Print a Pyramid of stars (char) using nested loops.. help me out with the algorithm
which language
using wich langauge [c++ or c#]
Where total is the total amount of stars you want to use for(int i = 0; i <= total; i++) { for(int j = 0; j <= total; j++) { print * } newline } this is the logic for a right angled triangle, it should be enough to help you solve the pyramid. Remember you'll need to use * and {SPACE}
int total = 9; //Am suposing its and odd number for(int left = total/2, int right = left + 1; left >=0 ; left--, right++ ) { int i = 0; for(; i < left; i++) cout << " "; for(i = left ; i < right; i++) cout << "*"; for(i = right; i < total; i++) cout << " "; cout << endl; } this should print: * *** ***** ******* ********* Theres an idea, prob it have some mistakes, hope it help u.
In Python: >>> def pyramid(n): ... for i in range(n): ... print(' '*(n-(i+1))+'*'*(2*i+1)) ... >>> pyramid(3) * *** ***** >>> pyramid(6) * *** ***** ******* ********* ***********
Read n for i from 1 to n do: for j from 1 to n - i Write ' ' for j from 1 to 2*i-1 Write '*' Write '/n'
Join our real-time social learning platform and learn together with your friends!