20 Pattern Programs

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

C++Intermediate
C++
#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:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

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

#include <iostream>

This header provides:

  • cout for displaying output
  • cin for taking input from the user

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.