C++ || Printing Various Patterns Using Nested Loops

Patterns

This page will demonstrate various programs which illustrates the use of nested loops to print selected patterns to the screen.

REQUIRED KNOWLEDGE FOR THIS PAGE

While Loops
For Loops
Using Nested Loops

The following are famous homework assignments which are usually presented in an entry level programming course.

There are a total of ten (10) different patterns on this page, which is broken up into sections. This page will list:

(4) methods of printing a triangle
(4) methods of printing an upside down triangle
(1) method which prints a square
(1) method which prints a giant letter 'X'

======= PRINTING A TRIANGLE =======

This program prints a triangle shape to the screen.


In the above example, the user has a choice of entering the number of rows which will be displayed to the screen

SAMPLE OUTPUT:

Enter a number: 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *

======= PRINTING A TRIANGLE WITH NUMBERS =======

The following program uses the same concept as above, but this time instead of using stars “*”, numbers will be printed to the screen.


SAMPLE OUTPUT:

Enter a number: 9
9
8 8
7 7 7
6 6 6 6
5 5 5 5 5
4 4 4 4 4 4
3 3 3 3 3 3 3
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1

======= PRINTING A TRIANGLE WITH NUMBERS IN-ORDER =======

The following program uses the same concept as above, but this time instead of using stars “*”, numbers will be printed to the screen in-order.


SAMPLE OUTPUT:

Enter a number: 9
9
8 9
7 8 9
6 7 8 9
5 6 7 8 9
4 5 6 7 8 9
3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

======= PRINTING A TRIANGLE WITH NUMBERS USING MULTIPLICATION =======

This example demonstrates another triangle, this time printing a multiplication table.


SAMPLE OUTPUT:

Enter a number: 9
9
9 18
9 18 27
9 18 27 36
9 18 27 36 45
9 18 27 36 45 54
9 18 27 36 45 54 63
9 18 27 36 45 54 63 72
9 18 27 36 45 54 63 72 81

======= PRINTING AN UPSIDE-DOWN TRIANGLE =======

This program is similar to the first one, this time printing the triangle upside-down.


SAMPLE OUTPUT:

Enter a number: 9
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

======= PRINTING AN UPSIDE-DOWN TRIANGLE WITH NUMBERS =======

This program is similar to the second one, this time printing the triangle upside-down.


SAMPLE OUTPUT:

Enter a number: 9
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

======= PRINTING AN UPSIDE-DOWN TRIANGLE WITH NUMBERS IN-ORDER =======

The following program uses the same concept as above, but this time instead of using stars “*”, numbers will be printed to the screen in-order.


SAMPLE OUTPUT:

Enter a number: 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9

===== PRINTING AN UPSIDE-DOWN TRIANGLE WITH NUMBERS USING MULTIPLICATION =====

This program is similar to the third one, this time printing the triangle upside-down.


SAMPLE OUTPUT:

Enter a number: 9
9 18 27 36 45 54 63 72 81
9 18 27 36 45 54 63 72
9 18 27 36 45 54 63
9 18 27 36 45 54
9 18 27 36 45
9 18 27 36
9 18 27
9 18
9

======= PRINTING A SQUARE =======

This program prints a square to the screen.


SAMPLE OUTPUT:

======= PRINTING THE LETTER “X” =======

The final program for this page will print a giant letter “X” to the screen.


SAMPLE OUTPUT:

And there you have it. Simple shapes made possible in C++.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

One Response to C++ || Printing Various Patterns Using Nested Loops

  1. the prototype abs is not understandable from me in printing of ‘X’
    plse give the sol. to print good morning in style of ‘X’

Leave a Reply