Printing a Row of Asterisks

Output row of Asterisks using a for-loop

Yes it is :-). You count from 0 to your value, right? So read your int before the loop and use the variable as the loop end condition:

Scanner input = new Scanner(System.in);
int num = input.nextInt();
for(int i =0; i<num; i++) {
System.out.print("*");
}

How do I get a for loop to print out asterisks that form a pyramid with odd numbered rows?

The main issue was not using the row variable in the condition check of the inner for loop, since the number of asterisks in each row is related to the row value.

The original logic does the same thing in every inner for loop, every time. 2*num<13 is basically the same as num<6.5, or really num<7 for ints, meaning 7 asterisks are printed for each row.

int numberOfRows = 7;
for (int row = 0; row < numberOfRows; row++)
{
for (int num = 0; num < 2 * row + 1; num++)
{
Console.Write("*");
}
Console.WriteLine();
}

FWIW, the only reason I even answered is to provide an example that's easier to read and maintain than the other answers. For example, here the row variable is named appropriately, illustrated by the use of a variable to contain the number of rows. If you want more rows, change numberOfRows. If you want a different number of asterisks on each row, change the condition in the inner for loop.

When dealing with for loops, it's a good idea to keep the names of variables used in the iterator and condition meaningful and easy to read, so that someone who reads it later (maybe even future you!) can more easily understand the logic.

How to print the right number of asterix in columns and rows?

There are several solutions:

1) Either you create two inner loops for each row: one to write the spaces and another to write the stars

    final int rows = 10;
for(int row = 1; row <= rows; row++) {
for(int i = 0; i < (rows - row); i++) {
System.out.print(" ");
}
for(int i = 0; i < (row); i++) {
System.out.print("*");
}
System.out.println();
}

2) Or you create one inner loops for each row and check the index to consider if you have to print a star or a blank.

final int rows = 10;
for(int row = 1; row <= rows; row++) {
for(int col = 1; col <= rows; col++) {
System.out.print((col <=(rows - row))? " " : "*");
}
System.out.println();
}

3) Or you can use string manipulation with subString (this is ugly but why not):

final int rows = 10;
final String stars = "************************";
final String blanks = " ";
for(int row = 1; row <= rows; row++) {
System.out.print(blanks.substring(0, rows - row));
System.out.println(stars.substring(0, row));
}

How to print text in a box of asterisks in an output file?

width = 30
message = "This is a test"
msg_ary = message.split(' ')
message1 = ' '.join(msg_ary[0: len(msg_ary) // 2]).center(width, ' ')
message2 = ' '.join(msg_ary[len(msg_ary) // 2:]).center(width, ' ')

print('*' * (width + 4))
print(f'* {message1} *')
print(f'* {message2} *')
print('*' * (width + 4))

> **********************************
> * This is *
> * a test *
> **********************************

Since this is homework, some explanation:

width = 30: Just some arbitrary width I chose, just as long as it fits your text.

msg_ary = ...... : Split the message by words (spaces) to make the break into to not split a word

messag1 = ...., message2 = .... : make the array in to a sentence again, note the handy call to "center" at the end : )

Error printing a pyramid pattern with asterisks in C [duplicate]

You got a logic errors for the second for loop which does not do what you want:

    // print stars:
for (j = 0; j < 2 * n + 1; j++) //<-- error #1 - n should be i as it's specific for this row
; //<-- error #2 - this termination is completely wrong, it means this `for` loop doing nothing
{


printf("*");

// go to new row
printf("\n");
}

which should be:

    // print stars:
for (j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
// go to new row
printf("\n");

What is wrong with this code to print a pattern of spaces and asterisks?

You only need to increase the size of the padding; try the following:

for row in range(1, 6):
print(' ' * (5 - row) * 2 + row * '* ')
# *
# * *
# * * *
# * * * *
# * * * * *

Or, you can use f-string with join:

for i in range(1, 6):
print(f"{' '.join('*' * i):>9}")
# *
# * *
# * * *
# * * * *
# * * * * *


Related Topics



Leave a reply



Submit