Crafting C++ Diamond Pattern

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to write a C++ program to print a diamond pattern using asterisks (*). We will create this program step by step.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") subgraph Lab Skills cpp/variables -.-> lab-96205{{"`Crafting C++ Diamond Pattern`"}} cpp/data_types -.-> lab-96205{{"`Crafting C++ Diamond Pattern`"}} cpp/operators -.-> lab-96205{{"`Crafting C++ Diamond Pattern`"}} cpp/for_loop -.-> lab-96205{{"`Crafting C++ Diamond Pattern`"}} end

Taking input from the user

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

We will take the number of rows as input from the user.

int rows;

cout << "Enter Diamond Star Pattern Row = ";
cin >> rows;

Displaying the pattern

To display the pattern, we will use nested loops. The outer loop will be responsible for the number of rows and the inner loop will print the asterisk symbols and spaces.

cout << "Diamond Star Pattern\n";

for (int i = 1; i <= rows; i++) {
    for (int j = 1; j <= rows - i; j++) {
        cout << " ";
    }

    for (int k = 1; k <= i * 2 - 1; k++) {
        cout << "*";
    }

    cout << "\n";
}

for (int i = rows - 1; i > 0; i--) {
    for (int j = 1; j <= rows - i; j++) {
        cout << " ";
    }

    for (int k = 1; k <= i * 2 - 1; k++) {
        cout << "*";
    }

    cout << "\n";
}

We use two loops to draw the diamond. We begin by drawing the top half of the diamond.

The outer loop (the first for loop) loops through each row of the diamond. We use a nested loop inside the outer loop.

The inner loops (the twofor loops) print the spaces and asterisks for each row of the diamond.

  • The first inner loop prints the correct number of spaces before the asterisks.
  • The second inner loop prints the asterisks.

We then draw the bottom half of the diamond. We begin by using a similar loop as used in the first half of the diamond. The outer loop counts down to zero (in reverse).

To compile and run the code, use the following command in the terminal:

g++ main.cpp -o main && ./main

Full code

Here is the full code for the Diamond pattern program.

#include <iostream>
using namespace std;

int main()
{
    int rows;

    cout << "Enter Diamond Star Pattern Row = ";
    cin >> rows;

    cout << "Diamond Star Pattern\n";

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }

        for (int k = 1; k <= i * 2 - 1; k++) {
            cout << "*";
        }

        cout << "\n";
    }

    for (int i = rows - 1; i > 0; i--) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }

        for (int k = 1; k <= i * 2 - 1; k++) {
            cout << "*";
        }

        cout << "\n";
    }

    return 0;
}

Summary

In this lab, we learned how to write a C++ program to print a diamond pattern using asterisks (*). We used nested loops to display the pattern. We began with a simple input and step by step created a C++ program that can be used to print a diamond using asterisks.

Other C++ Tutorials you may like