Diamond Pattern

Diamond Pattern in C++ (3 Programs With Output)

C++Intermediate
C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    
    cout << "Enter number of rows (half): ";
    cin >> rows;
    
    // Upper part of diamond
    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;
    }
    
    // Lower part of diamond
    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

Enter number of rows (half): 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

This program teaches you how to print a diamond pattern in C++ using nested loops. A diamond pattern is created by combining an upper pyramid (increasing stars) and a lower inverted pyramid (decreasing stars). This is a classic pattern printing problem that demonstrates advanced loop control and pattern recognition.


1. What This Program Does

The program prints a diamond pattern based on the number of rows entered. For example, with 5 rows (half-diamond), it creates:

  • Upper part: increasing stars forming a pyramid
  • Lower part: decreasing stars forming an inverted pyramid
  • Combined: forms a diamond shape

The diamond is symmetric - the upper and lower halves mirror each other.


2. Header File Used

#include <iostream>

This header provides:

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

3. Understanding Diamond Pattern

Structure:

A diamond consists of two parts:

  • Upper pyramid: stars increase from 1 to maximum
  • Lower pyramid: stars decrease from maximum-1 to 1

Key Insight:

  • The middle row has the maximum number of stars
  • Upper and lower parts are symmetric
  • Total rows = 2 * (input rows) - 1

Example (input = 5):

  • Upper: 1, 3, 5, 7, 9 stars (5 rows)
  • Lower: 7, 5, 3, 1 stars (4 rows, excluding middle)
  • Total: 9 rows forming diamond

4. Declaring Variables

The program declares: int rows;

  • rows stores the number of rows for half the diamond.
  • The full diamond will have (2 * rows - 1) total rows.

5. Taking Input From the User

The program asks: cout << "Enter number of rows (half): "; cin >> rows;

The user enters a number, for example: 5 This represents half the diamond (upper part).


6. Upper Part of Diamond

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; }

How it works:

  • Outer loop (i): iterates from 1 to rows (upper half)
  • First inner loop: prints (rows - i) spaces for centering
  • Second inner loop: prints (2*i - 1) stars (odd numbers: 1, 3, 5, ...)
  • Creates increasing pyramid from top

Output (for rows = 5, upper part):

*

*

***

## *


7. Lower Part of Diamond

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; }

How it works:

  • Outer loop (i): iterates from (rows - 1) down to 1 (decreasing)
  • Starts from rows - 1 to avoid repeating the middle row
  • First inner loop: prints (rows - i) spaces for centering
  • Second inner loop: prints (2*i - 1) stars (decreasing: 7, 5, 3, 1)
  • Creates decreasing inverted pyramid

Output (for rows = 5, lower part):

***

*


*

8. Complete Diamond Pattern

Combining both parts creates the full diamond:

Upper Part (rows 1-5):

*

*

***

## *

Lower Part (rows 6-9):

***

*


*

Complete Diamond:

*

*

***

## *

***

*


*

9. Understanding the Algorithm

Why rows - 1 in lower loop?:

  • Upper part goes from 1 to rows (includes middle row)
  • Lower part should start from rows - 1 (excludes middle row)
  • This prevents duplicating the middle row

Formula 2*i - 1:

  • Ensures odd number of stars per row
  • Row 1: 1 star, Row 2: 3 stars, Row 3: 5 stars, etc.
  • Creates symmetric diamond shape

Spacing Logic:

  • (rows - i) spaces center each row
  • More spaces at top, fewer at middle
  • Symmetric spacing in upper and lower parts

10. Other Patterns (Mentioned but not shown in code)

Hollow Diamond:

  • Print stars only at edges
  • Use conditional: if (j == 1 || j == 2*i-1 || i == 1 || i == rows)
  • Creates outline of diamond

Number Diamond:

  • Replace stars with numbers
  • Numbers can increase, decrease, or follow patterns
  • Creates numeric diamond shape

11. When to Use Diamond Patterns

Educational Purposes:

  • Advanced nested loop practice
  • Understanding symmetric patterns
  • Developing pattern recognition skills

Interview Preparation:

  • Common coding interview problem
  • Tests understanding of loops and patterns
  • Demonstrates logical thinking

Visual Programming:

  • Creating geometric shapes
  • ASCII art generation
  • Pattern-based graphics

12. Important Considerations

Row Calculation:

  • Input rows = half diamond size
  • Total rows = 2 * rows - 1
  • Middle row is at position: rows

Loop Boundaries:

  • Upper: i from 1 to rows (inclusive)
  • Lower: i from rows - 1 to 1 (decreasing)
  • Careful with boundaries to avoid duplication

Spacing:

  • Single space " " for centering
  • Consistent spacing creates symmetric appearance
  • Test with different row counts

13. return 0;

This ends the program successfully.


Summary

  • Diamond pattern combines upper pyramid and lower inverted pyramid.
  • Upper part: stars increase from 1 to maximum (2*rows - 1).
  • Lower part: stars decrease from (2*(rows-1) - 1) to 1, starting from rows - 1.
  • Formula 2*i - 1 ensures odd number of stars per row.
  • Spacing (rows - i) centers each row symmetrically.
  • Understanding symmetric patterns and loop control is essential.
  • Variations include hollow diamond and number diamond.
  • Diamond patterns demonstrate advanced nested loop mastery.

This program is fundamental for beginners learning advanced pattern printing, understanding symmetric patterns, and preparing for complex pattern problems in C++ programs.