Simply increase your step size!
for stepSize in range(10):
for count in range(10):
print((count + 1) * (stepSize + 1), end=" ")
# count loop has ended, back into the scope of stepSize loop
# We are also printing(" ") to end the line
print(" ")
# stepSize loop has finished, code is done
Explanation:
The first, outer loop is increasing our step size, then for each step size we count up 10 steps and finish the line when we print(" ") in the outer for loop.
Simply increase your step size!
for stepSize in range(10):
for count in range(10):
print((count + 1) * (stepSize + 1), end=" ")
# count loop has ended, back into the scope of stepSize loop
# We are also printing(" ") to end the line
print(" ")
# stepSize loop has finished, code is done
Explanation:
The first, outer loop is increasing our step size, then for each step size we count up 10 steps and finish the line when we print(" ") in the outer for loop.
This is how I would do it:
for x in range (1,11):
product = []
for y in range (1, 11):
current_product = x * y
product.append(current_product)
print(*product, sep=' ')
for (int i = 1; i <= 4; i++){ for (int j = 1; j <= 12; j++){ System.out.print("~"); } } System.out.println();
can easily be:
for (int i = 0; i < 48; i++) {
System.out.print('~');
}
System.out.println();
Some edits:
- Improved formatting:
){to) { - Simplified to one
forloop that runs from0to47(start at0, run to less than48) - Print a single char instead of a String with one char in it to improve performance
This:
System.out.print("~"); for (int i = 1; i <=15; i++){ System.out.print("+~~"); } System.out.println("+~");
Can be this:
for (int i = 0; i < 16; i++){
System.out.print("~+~");
}
System.out.println();
What changed:
- Better formatting
- Loops and prints the
~+~pattern
System.out.print("+~"); for (int i = 1; i <= 15; i++){ System.out.print("++~"); } System.out.println("+");
to:
for (int i = 0; i < 16; i++){
System.out.print("+~+");
}
System.out.println();
Changes made:
- Better formatting
- Loops and prints the
+~+pattern
All of the fixes have some things in common:
- Indentation and formatting
- Find the correct pattern
Other changes:
Use methods:
Currently all your code is in the
mainmethod. Split it up.
Final code:
public class Exercises {
public static void main (String[] args){
printTildes();
printTPT_Pattern();
printPTP_Pattern();
printTildes();
}
private static void printTildes() {
for (int i = 0; i < 48; i++){
System.out.print("~");
}
System.out.println();
}
private static void printTPT_Pattern() {
for (int i = 0; i < 16; i++){
System.out.print("~+~");
}
System.out.println();
}
private static void printPTP_Pattern() {
for (int i = 0; i < 16; i++){
System.out.print("+~+");
}
System.out.println();
}
}
You're not breaking up the patterns in the most logical way. Each line just consists of repeating blocks of three characters.
public class SquigglePlus {
private static final String[] PATTERNS = { "~~~", "~+~", "+~+", "~~~" };
public static void main(String[] args) {
for (int line = 0; line < PATTERNS.length; line++) {
for (int col = 0; col < 48; col += PATTERNS[line].length()) {
System.out.print(PATTERNS[line]);
}
System.out.println();
}
}
}
Alternatively, the inner loop could just count to 16: for (int i = 0; i < 16; i++) { … }. Note that in Java, that is the most idiomatic way to count: start with 0, and use < for the termination check. The other way (for (int i = 1; i <= 16; i++) { … }) would not be wrong, but usually you would write it that way only if you had a special reason.
Better yet, use a modern enhanced for-loop instead of the old-style counting loop.
public class SquigglePlus {
private static final String[] PATTERNS = { "~~~", "~+~", "+~+", "~~~" };
public static void main(String[] args) {
for (String pattern : PATTERNS) {
for (int col = 0; col < 48; col += pattern.length()) {
System.out.print(pattern);
}
System.out.println();
}
}
}
The odd integer constants in the OP code don't make a lot of sense (even if they might work.)
One variable counting up while another counts down can be a bit like Zero Mostel's "One long staircase just going up, and one even longer coming down.." Hard to envision.
Try the following:
#include <stdio.h>
int main() {
for( int r = 1; r < 7; r++ ) { // output 6 rows
for( int b = r; b <= 7; b++ ) // 1=>7, then 2=>7, then 3=>7... easy!
putchar( 'B' ); // no need to engage all of printf() for a character
putchar( '\n' );
}
return 0;
}
BBBBBBB
BBBBBB
BBBBB
BBBB
BBB
BB
In essence, one can imagine that the floor (the starting point) gets progressively higher, but the ceiling does not move.
Noticed that you ask, in the comments below your question, "What is the correct, efficient way...? "
Here is one way that does not use nested loops:
int main() {
char *bees = "BBBBBBB";
for( int i = 0; bees[i+1]; i++ ) // "+1" because pattern ends with "BB"
puts( bees + i );
return 0;
}
It is much easier if you use functions and can test it with different numbers.
void printPattern(unsigned nrows)
{
for(unsigned row = nrows; row > 0; row--)
{
for(unsigned col = 0; col <= row; col++)
printf("B");
printf("\n");
}
}
int main()
{
printPattern(7);
printf("---------------------\n");
printPattern(3);
printf("---------------------\n");
printPattern(1);
printf("---------------------\n");
printPattern(0);
printf("---------------------\n");
printPattern(15);
printf("---------------------\n");
}
https://godbolt.org/z/xjzMPczPE