Jump to content

Draw a number of triangles (nested while loops)

- - - - -

  • Please log in to reply
10 replies to this topic

#1
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
So I understand how to draw a line of asterisks, square, and make an individual right triangle, but how would I draw a number of right triangles, from the smallest one (height of 1) to the largest (height of 'x'); e.g., if the entered 'x' is 5,

*

*
**

*
**
***

*
**
***
****

*
**
***
****
*****

---------- Post added at 03:36 PM ---------- Previous post was at 03:31 PM ----------

Here is the code for the three other drawings.
For some reason the indents get removed when I come from IDE

[FONT=Courier New]  [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.util.Scanner;

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab06
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] x;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] row_count;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] col_count;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] tri_count;

Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Line of asterisks
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer greater than 0 for length of line: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x = keyboard.nextInt();

System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Line of "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + x + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" asterisks"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (0 < x)
{
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x--;
}
System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"\n"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]); 

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Square block of asterisk
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer greater than 0 for size of square: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x = keyboard.nextInt();

row_count = x;
col_count = x;

System.out.println(x + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" by "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + x + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" square of asterisks"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);


[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (0 < col_count)
{
row_count = x;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (0 < row_count)
{
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
row_count--;
}
System.out.println();
col_count--;
}
System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"\n"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Upside-down right triangle
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer greater than 0 for size of upside-down right triangle: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x = keyboard.nextInt();

row_count = x;
col_count = x;

System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Base of upside-down right triangle is "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + x);


[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (0 < col_count)
{
row_count = col_count;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (0 < row_count)
{
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
row_count--;
}
System.out.println();
col_count--;
}

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Number of right triangles
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer greater than 0 for the number of triangles (w/ increasing base size): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x = keyboard.nextInt();

tri_count = x;

System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Number of triangles (w/ increasing base size) is "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + x);







}
}
[/FONT]



#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You seem to understand nested loops well enough. To perform what you mentioned at the beginning, you'll need another nested loop structure, but this time 3 levels deep instead of two. (Outer loop controls the number of triangles drawn, and increments the value which determines the size of the current triangle, middle loop and inner loop draw a triangle of the correct size, as you've already done in your code above.)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts

gregwarner said:

You seem to understand nested loops well enough. To perform what you mentioned at the beginning, you'll need another nested loop structure, but this time 3 levels deep instead of two. (Outer loop controls the number of triangles drawn, and increments the value which determines the size of the current triangle, middle loop and inner loop draw a triangle of the correct size, as you've already done in your code above.)

I sort of get what you're saying but not completely.
Can you write pseudocode?

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Using for loops:

Declare variables:

num_of_triangles

triangle_count

line_count

asterisk_count


// Get number of triangles to print from the user.

// Store this in num_of_triangles.


for triangle_count from 1 to num_of_triangles:

    for line_count from 1 to triangle_count:

        for asterisk_count from 1 to line_count:

            print '*'.

        next asterisk_count.

        print end line.

    next line_count.

    print end line.

next triangle_count.


The outer loop prints a collection of triangles. The middle loop prints one triangle. The inner loop prints one line of one triangle.

or, using While loops like you've been doing so far:

Declare variables:

num_of_triangles

triangle_count

line_count

asterisk_count


// Get number of triangles to print from the user.

// Store this in num_of_triangles.


triangle_count = 0.

while triangle_count < num_of_triangles:

    line_count = 0.

    while line_count < triangle_count:

        asterisk_count = 0.

        while asterisk_count < line_count:

            print '*'.

        end while.

        print end line.

        line_count++

    end while.

    print end line.

    triangle_count++

end while.



Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts

gregwarner said:

Using for loops:
Declare variables:
num_of_triangles
triangle_count
line_count
asterisk_count

// Get number of triangles to print from the user.
// Store this in num_of_triangles.

for triangle_count from 1 to num_of_triangles:
    for line_count from 1 to triangle_count:
        for asterisk_count from 1 to line_count:
            print '*'.
        next asterisk_count.
        print end line.
    next line_count.
    print end line.
next triangle_count.

The outer loop prints a collection of triangles. The middle loop prints one triangle. The inner loop prints one line of one triangle.

or, using While loops like you've been doing so far:
Declare variables:
num_of_triangles
triangle_count
line_count
asterisk_count

// Get number of triangles to print from the user.
// Store this in num_of_triangles.

triangle_count = 0.
while triangle_count < num_of_triangles:
    line_count = 0.
    while line_count < triangle_count:
        asterisk_count = 0.
        while asterisk_count < line_count:
            print '*'.
        end while.
        print end line.
        line_count++
    end while.
    print end line.
    triangle_count++
end while.

This is what I got but it doesnt seem to work
[FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Number of right triangles
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer greater than 0 for the number of triangles (w/ increasing base size): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x = keyboard.nextInt(); [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//num_of_triangles
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
tri_count = 0; [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//triangle count .. 
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Number of triangles (w/ increasing base size) is "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + x);

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (tri_count < x)
{
col_count = 0; [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//line_count
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (col_count < tri_count)
{
row_count = 0; [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//asterisk count
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (row_count < col_count)
{
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
}
}
System.out.println();
tri_count++;
}
[/FONT]

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You need to manually increment row_count and col_count since you're using while loops.
You're also missing a println() in there somewhere. Compare to my pseudocode and see if you can figure out where.

Edit:
I'm sorry, I don't know where my head was. Write it like this:


triangle_count = 1.

while triangle_count <= num_of_triangles:

    line_count = 1.

    while line_count <= triangle_count:

        asterisk_count = 1.

        while asterisk_count <= line_count:

            print '*'.

            asterisk_count++

        end while.

        print end line.

        line_count++

    end while.

    print end line.

    triangle_count++

end while.


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#7
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts

gregwarner said:

You need to manually increment row_count and col_count since you're using while loops.
You're also missing a println() in there somewhere. Compare to my pseudocode and see if you can figure out where.

Edit:
I'm sorry, I don't know where my head was. Write it like this:

triangle_count = 1.
while triangle_count <= num_of_triangles:
    line_count = 1.
    while line_count <= triangle_count:
        asterisk_count = 1.
        while asterisk_count <= line_count:
            print '*'.
            asterisk_count++
        end while.
        print end line.
        line_count++
    end while.
    print end line.
    triangle_count++
end while.

This just continues to print * in a straight line

[FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Number of right triangles
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer greater than 0 for the number of triangles (w/ increasing base size): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
x = keyboard.nextInt(); [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//num_of_triangles
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
tri_count = 1; [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//triangle count .. 
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]
System.out.println([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Number of triangles (w/ increasing base size) is "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + x);

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (tri_count <= x)
{
col_count = 1; [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//line_count
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (col_count <= tri_count)
{
row_count = 1; [/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]//asterisk count
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (row_count <= col_count)
{
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
}
System.out.println();
tri_count++;
}
}
[/FONT]


#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

gregwarner said:

You need to manually increment row_count and col_count since you're using while loops.

You're not incrementing these variables gtghost.

#9
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Look carefully at your code and compare with my pseudocode. You're missing one println() statement somewhere, and you're also missing a couple of increment statements.

Here's a hint: Whenever you use while loops, you need to manually increment or decrement the test variable yourself. They're not like for loops in that this functionality is not inherently built in. The body of your while loops must contain some code to update the value of your test variable, or else it will run indefinitely as you are currently experiencing.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#10
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
I got everything to work. Thanks for all the help
[FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] java.util.Scanner;

[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] [/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] Lab06
{
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] [/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] [/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] main(String[] args)
{
[/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] x;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] row_count;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] col_count;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] tri_count;

Scanner keyboard = [/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] Scanner(System.in);

[/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Line of asterisks
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]System.out.print([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter an integer (>0): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);
x = keyboard.nextInt();

System.out.println([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Line of "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] + x + [/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" asterisks"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);

col_count = 1; 
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (col_count <= x)
{
System.out.print([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);
col_count++;
}
System.out.println([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"\n"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]); 

[/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Square block of asterisk
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]System.out.println(x + [/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" by "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] + x + [/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" square of asterisks"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);

row_count = 1;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (row_count <= x)
{
col_count = 1;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (col_count <= x)
{
System.out.print([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);
col_count++;
}
System.out.println();
row_count++;
}
System.out.println([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"\n"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]); 

[/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Upside-down right triangle
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]System.out.println([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Upside-down right triangle of "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] + x);

col_count = 1;
tri_count = x;

[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (col_count <= x)
{
row_count = 1;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (row_count <= tri_count)
{
System.out.print([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);
row_count++;
}
tri_count--;
System.out.println();
col_count++;
}
System.out.println([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"\n"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);

[/COLOR][/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Several right triangles
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]System.out.println([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Number of triangles (w/ increasing base size) is "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] + x);

tri_count = 1;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (tri_count <= x)
{
col_count = 1;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (col_count <= tri_count)
{
row_count = 1;
[/COLOR][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (row_count <= col_count)
{
System.out.print([/COLOR][/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"*"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]);
row_count++;
}
col_count++;
System.out.println();
}
tri_count++;
System.out.println();
}
}
}[/COLOR][/FONT]


#11
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You're welcome.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users