Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

20 Pattern Programs in C++

Print five classic star patterns in one C++ program with rows fixed at 5: right half, left half, full, inverted, and diamond. A loop formula cheat sheet.

IntermediateAdvanced Pattern ProgramsExample 6 of 6
20-pattern-programs.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int rows = 5;
6
7 // Pattern 1: Right Half Pyramid
8 cout << "Pattern 1 - Right Half Pyramid:" << endl;
9 for (int i = 1; i <= rows; i++) {
10 for (int j = 1; j <= i; j++) {
11 cout << "* ";
12 }
13 cout << endl;
14 }
15
16 // Pattern 2: Left Half Pyramid
17 cout << "\nPattern 2 - Left Half Pyramid:" << endl;
18 for (int i = 1; i <= rows; i++) {
19 for (int j = 1; j <= rows - i; j++) {
20 cout << " ";
21 }
22 for (int j = 1; j <= i; j++) {
23 cout << "* ";
24 }
25 cout << endl;
26 }
27
28 // Pattern 3: Full Pyramid
29 cout << "\nPattern 3 - Full Pyramid:" << endl;
30 for (int i = 1; i <= rows; i++) {
31 for (int j = 1; j <= rows - i; j++) {
32 cout << " ";
33 }
34 for (int j = 1; j <= 2 * i - 1; j++) {
35 cout << "*";
36 }
37 cout << endl;
38 }
39
40 // Pattern 4: Inverted Pyramid
41 cout << "\nPattern 4 - Inverted Pyramid:" << endl;
42 for (int i = rows; i >= 1; i--) {
43 for (int j = 1; j <= rows - i; j++) {
44 cout << " ";
45 }
46 for (int j = 1; j <= 2 * i - 1; j++) {
47 cout << "*";
48 }
49 cout << endl;
50 }
51
52 // Pattern 5: Diamond
53 cout << "\nPattern 5 - Diamond:" << endl;
54 for (int i = 1; i <= rows; i++) {
55 for (int j = 1; j <= rows - i; j++) {
56 cout << " ";
57 }
58 for (int j = 1; j <= 2 * i - 1; j++) {
59 cout << "*";
60 }
61 cout << endl;
62 }
63 for (int i = rows - 1; i >= 1; i--) {
64 for (int j = 1; j <= rows - i; j++) {
65 cout << " ";
66 }
67 for (int j = 1; j <= 2 * i - 1; j++) {
68 cout << "*";
69 }
70 cout << endl;
71 }
72
73 return 0;
74}

Sample Output

Pattern 5 - Diamond:

The last block; 9 lines tall.

In this one

  • How five common patterns differ only in their inner loops
  • How a hardcoded rows value replaces cin
  • How the diamond reuses the full and inverted pyramids
  • How to adapt the file by changing one variable

How it works

  1. 1

    rows is set to 5 in code; no input.

  2. 2

    Patterns 1 and 2 print half pyramids.

  3. 3

    Patterns 3 and 4 print centered pyramids.

  4. 4

    Pattern 5 prints a diamond from two loops.

Edge cases

Change rows to 1

Set int rows = 1;

Each pattern prints a single star, and the diamond lower half does not run.

Expecting twenty patterns

Read the title and count the output.

The code prints five patterns. The other pages in this topic add triangles, hollow shapes, and number patterns.

What's going on

The file is named like a gallery. The code actually prints five patterns, rows hardcoded to 5. No cin.

1.Right-half: j <= i, "* "
2.Left-half: pad " " (rows - i) times, then stars
3.Full: pad " ", then 2 * i - 1 times "*"
4.Inverted full: i counts down
5.Diamond: full pyramid, then inverted from rows - 1 so the middle isn't doubled

Those are the Pattern Programs pages plus Diamond, concatenated with labels.

Change rows at the top to try 3 or 7. The formulas don't change.

If you came here for twenty separate homeworks, this source only has those five. The earlier pages in this topic (star, triangle, pyramid, number) cover the rest of the usual set.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

How many separate patterns does this code print?

Pick an answer to check it.

Question 2

Which pattern uses two outer loops?

Pick an answer to check it.

Questions people ask

Why no cin?

rows is hardcoded so the whole output is predictable for revision.

Where are the other patterns?

Star Pattern, Triangle Pattern, Pyramid Program, and the number pattern page cover the rest.

Can I add my own pattern?

Yes, add another labeled block with its own loops at the end of main.