Using Nested While Loop to Print Pyramid of Stars

Pyramid of stars using while loops python nested while loops

Since this is homework, rather than give you a code answer, I'll give you an outline of how this should work. Given the constraint of three loops, here is what seems to be expected:

  1. The first, outer loop should iterate over the rows. The first pass generates the first row, the second pass generates the second row, etc.

  2. The second loop is nested inside the first loop. It generates the leading spaces for the current row.

  3. The third loop is also nested inside the first loop, but not the second. It is executed after the second loop, and should follow it in your code. It generates the stars for the current row.

Each of these loops is very simple and you should have no trouble with them. The only tricky part is getting print to print a string without following it with a newline character (which ends the current line). If you're using Python 3, you can do this with print("abc", end="") This will print the string abc but will not end the line. After the third loop, you will need to end the line, which you can do with print().

Logic behind printing pyramid shape using nested for loops in Java

Nested for loops like that are used when you have pseudo code like this:

Do the following x times:
Do the following y times:
some stuff
Do the following z times:
some stuff

In your specific case, the size of the pyramid is dynamic and is stored in a variable called size. To print a pyramid, you have to print size times the following thing:

  • Some whitespace and some *

How do you print that then? You calculate how many white spaces and * should there be and print them. Since the numbers of whitespaces and * are dynamic, you need a for loop to do this, instead of hardcoding them in.

Do you see the structure now?

The outer loop prints each layer of the pyramid. The first inner loop prints the white spaces and the second inner loop prints the *.

How do I format this nested for loop pyramid to look like how I need it to?

You don't need to skip lines in order to print an odd number of zeros. Use 2*i+1.

2*i will always be an even number, so 2*i+1 is always an odd one.

Now you just need to add space before to print the zeros. To do that you use another loop which prints less spaces as i grows bigger. You could use the condition x-i.

#include <stdio.h>

int main(void)
{

int x;

printf("How many rows do you want in the pyramid? (newlines included):\n");
scanf("%d", &x);

for (int i = 0; i < x; i++) {

for (int j = 1; j < x-i; j++) {
printf(" ");
}

for (int j = 0; j < 2*i+1; j++) {
printf("0");
}
printf("\n");
}
return 0;
}

nested for loop to build a 0 pyramid

You are using print with end="" to build the lines character by character without getting to a new line.

To build the pyramid, you need to skip to the next line after a line of zeros, thus the print(" ") that has a end='\n' parameter by default

output without the last print:

       0      000     00000    0000000   000000000  00000000000 0000000000000

Here is a modification of the code to show you where the separators are:

for i in range(0,7):
for j in range(0, 7 - i):
print(" ", end = "-")
for k in range(0, 2 * i + 1):
print("0", end = "+")
print("", end='=\n')

output:

 - - - - - - -0+=
- - - - - -0+0+0+=
- - - - -0+0+0+0+0+=
- - - -0+0+0+0+0+0+0+=
- - -0+0+0+0+0+0+0+0+0+=
- -0+0+0+0+0+0+0+0+0+0+0+=
-0+0+0+0+0+0+0+0+0+0+0+0+0+=

Here is a shorter alternative to the initial code to build the lines directly with a single loop:

n=7
for i in range(n):
print(' '*(n-i)+'0'*(2*i+1))

output:

       0
000
00000
0000000
000000000
00000000000
0000000000000

How to make this numbers triangle using nested while loops? (python 2.7)

Do you require nested loops?

>>> n=5
>>> for i in range(1, n+1):
... print("{:>{width}}".format(str(i)*i, width=n))
1
22
333
4444
55555

But to fix your code - you are missing the multiplier on your number:

n=5
m=1
while n>=1:
while m<=5:
print " "*(n), str(m)*m
n=n-1
m=m+1

Create a pyramid of integers in python using nested for loop

Jared's answer is correct and definitely more elegant, but here's another solution anyway. More beginner friendly hopefully easier to understand at a glance.

def pyramid(number_of_rows):
for i in range(1, number_of_rows + 1):
indent = ' ' * (number_of_rows - i)
row = str(i) * (i * 2 - 1)
print(indent + row)

C programming nested for loop print half pyramid

This is a simple question and the correct code for this is:

int main()
{
int height, i, row, j,no=1;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i > 0; i--) //print the number of rows based on input
{
if(no>3)
no=1;//so that numbers never exceed 3
for (row = 0; row < i-1; row++)
printf(" "); // print spaces for each rows
for(j=height+1;j>i;--j)
printf("%d",no);//print the numbers
no++;
printf("\n");
}
return 0;
}


Related Topics



Leave a reply



Submit