20 Pattern Programs

20 Pattern Programs in C++ (With Code & Output)

IntermediateTopic: Advanced Pattern Programs
Back

C++ 20 Pattern Programs Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    
    // Pattern 1: Right Half Pyramid
    cout << "Pattern 1 - Right Half Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Pattern 2: Left Half Pyramid
    cout << "\nPattern 2 - Left Half Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << "  ";
        }
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Pattern 3: Full Pyramid
    cout << "\nPattern 3 - Full Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Pattern 4: Inverted Pyramid
    cout << "\nPattern 4 - Inverted Pyramid:" << endl;
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Pattern 5: Diamond
    cout << "\nPattern 5 - Diamond:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    for (int i = rows - 1; i >= 1; i--) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    return 0;
}
Output
Pattern 1 - Right Half Pyramid:
*
* *
* * *
* * * *
* * * * *

Pattern 2 - Left Half Pyramid:
        *
      * *
    * * *
  * * * *
* * * * *

Pattern 3 - Full Pyramid:
    *
   ***
  *****
 *******
*********

Pattern 4 - Inverted Pyramid:
*********
 *******
  *****
   ***
    *

Pattern 5 - Diamond:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Understanding 20 Pattern Programs

This program is a comprehensive collection of 20 different pattern programs in C++. Pattern printing is one of the best ways to master nested loops, understand loop control, and develop logical thinking. This collection covers all fundamental pattern types: pyramids, triangles, diamonds, squares, rectangles, hollow patterns, number patterns, and alphabet patterns.

---

1. What This Program Does

The program demonstrates 20 different pattern printing techniques, each showcasing different loop strategies and pattern formation logic. Patterns include:

Basic shapes: pyramids, triangles, diamonds
Geometric shapes: squares, rectangles
Special patterns: hollow patterns, number patterns, alphabet patterns

Each pattern teaches different aspects of nested loop control and pattern recognition.

---

2. Header File Used

This header provides:

cout for displaying output
cin for taking input from the user

---

#include <iostream>

3. Understanding Pattern Collections

Why Learn Multiple Patterns?

:

Each pattern teaches different loop techniques
Develops pattern recognition skills
Builds understanding of nested iterations
Essential for programming interviews

Pattern Categories

:

Star Patterns: pyramids, triangles, diamonds
Number Patterns: sequences, Floyd's triangle
Alphabet Patterns: letter sequences
Hollow Patterns: outline shapes
Geometric Patterns: squares, rectangles

---

4. Key Patterns Included

Pattern 1-5: Basic Star Patterns

Right Half Pyramid: increasing stars
Left Half Pyramid: right-aligned stars
Full Pyramid: centered pyramid
Inverted Pyramid: upside-down pyramid
Diamond: combination of two pyramids

Pattern 6-10: Advanced Star Patterns

Hollow Pyramid: stars only on edges
Hourglass: inverted + normal pyramid
Arrow: directional patterns
Cross: geometric cross shape
Plus: plus sign pattern

Pattern 11-15: Number Patterns

Increasing numbers: 1, 1 2, 1 2 3, ...
Same number: 1, 2 2, 3 3 3, ...
Floyd's Triangle: continuous sequence
Number Pyramid: numbers in pyramid
Pascal's Triangle: mathematical pattern

Pattern 16-20: Special Patterns

Alphabet patterns: A, A B, A B C, ...
Square patterns: filled and hollow
Rectangle patterns: various sizes
Hollow patterns: outline shapes
Special shapes: custom patterns

---

5. Common Pattern Techniques

Nested Loops

:

Outer loop: controls rows
Inner loops: control columns and spacing
Multiple inner loops for complex patterns

Spacing Logic

:

Spaces for alignment: (rows - i) spaces
Spaces for centering: calculated based on row
Understanding spacing is crucial

Character Selection

:

Stars (*) for visual patterns
Numbers for number patterns
Letters for alphabet patterns
Conditional printing for hollow patterns

---

6. When to Use Pattern Programs

Educational Purposes

:

Learning nested loops systematically
Understanding loop control
Developing logical thinking
Pattern recognition skills

Interview Preparation

:

Common coding interview questions
Tests understanding of loops
Demonstrates problem-solving ability

Skill Development

:

Builds programming fundamentals
Develops attention to detail
Improves debugging skills

---

7. Important Considerations

Loop Boundaries

:

Careful with <= vs <
Starting from 1 vs 0
Inclusive vs exclusive ranges

Formula Understanding

:

2*i - 1 for odd numbers
rows - i for spacing
Understanding formulas is key

Pattern Variations

:

Small changes create different patterns
Experiment with formulas
Practice with different row counts

---

8. Summary

Pattern collections teach comprehensive loop techniques.
20 patterns cover all fundamental pattern types.
Each pattern demonstrates different loop strategies.
Understanding spacing and formulas is essential.
Pattern printing develops logical thinking and loop mastery.
Essential for programming interviews and skill development.
Practice with variations to master pattern printing.

This comprehensive collection is fundamental for beginners learning nested loops, understanding pattern formation, and preparing for programming interviews and advanced pattern problems in C++ programs.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for 20 Pattern Programs

This C++ program is part of the "Advanced Pattern Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents