C++ Program to Print Hollow Star Pyramid in a Diamond Shape

Creating a hollow star pyramid in a diamond shape is an interesting exercise to practice nested loops and conditional statements in C++. This pattern involves printing a hollow diamond made of stars (*), where the diamond consists of two parts: the upper pyramid and the inverted lower pyramid, both hollow except for their borders.

Example: Hollow Star Pyramid in a Diamond Shape

This program will prompt the user to enter the number of rows for the upper part of the diamond. The lower part will automatically be generated to match the upper part, creating a symmetrical diamond shape.

C++ Code:

#include <iostream>

using namespace std;

int main() {

    int rows;

    // User inputs the number of rows for the upper part of the diamond

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the upper part of the diamond

    for (int i = 1; i <= rows; ++i) {

        // Print leading spaces

        for (int j = 1; j <= rows – i; ++j) {

            cout << ” “;

        }

        // Print stars and hollow spaces

        for (int j = 1; j <= 2 * i – 1; ++j) {

            if (j == 1 || j == 2 * i – 1) {

                cout << “*”;

            } else {

                cout << ” “;

            }

        }

        cout << endl;

    }

    // Print the lower part of the diamond

    for (int i = rows – 1; i > 0; –i) {

        // Print leading spaces

        for (int j = 1; j <= rows – i; ++j) {

            cout << ” “;

        }

        // Print stars and hollow spaces

        for (int j = 1; j <= 2 * i – 1; ++j) {

            if (j == 1 || j == 2 * i – 1) {

                cout << “*”;

            } else {

                cout << ” “;

            }

        }

        cout << endl;

    }

    return 0;

}

Explanation:

  1. Header and Namespace: Includes <iostream> for input and output operations and uses the std
  2. Input: The user specifies the number of rows for the upper half of the diamond.
  3. Upper Diamond:
    • Outer Loop: Manages the rows of the upper pyramid.
    • First Inner Loop: Controls the spaces before the stars begin, decreasing as the rows increase.
    • Second Inner Loop: Handles the printing of stars and hollow spaces. Stars are printed only at the edges (j == 1 or j == 2 * i – 1), leaving the rest as spaces.
  4. Lower Diamond:
    • Outer Loop: Manages the rows of the inverted lower pyramid.
    • First Inner Loop: Similar to the upper part but for spacing, it increases as you move deeper into the lower part.
    • Second Inner Loop: Similar to the upper part in terms of star placement and hollow spaces.

Program Output:

If the user inputs 4 for the number of rows, the output will be:

*

* *

*   *

*     *

*   *

* *

*

This pattern creates a visually appealing hollow diamond using nested loops and conditional checks to determine where to print stars and spaces, illustrating how to manipulate console output effectively in C++.

C++ Program to Print Inverted Hollow Star Pyramid Pattern

Creating an inverted hollow star pyramid pattern in C++ is an excellent exercise to deepen your understanding of loops and conditional statements. This pattern features an inverted pyramid where only the border of the pyramid is marked with stars (*), leaving the inside hollow or empty.

Example: Inverted Hollow Star Pyramid Pattern

This program will display an inverted hollow star pyramid based on the number of rows specified by the user. The top row will be completely filled with stars, and subsequent rows will only have stars at the borders, with the inner part being hollow until the last row which has just one star.

C++ Code:

#include <iostream>

using namespace std;

int main() {

    int rows;

    // User inputs the number of rows for the pyramid

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the inverted hollow pyramid

    for (int i = rows; i > 0; –i) {

        // Print leading spaces

        for (int j = 0; j < rows – i; ++j) {

            cout << ” “;

        }

        // Print stars and hollow spaces

        for (int j = 1; j <= 2 * i – 1; ++j) {

            if (j == 1 || j == 2 * i – 1 || i == rows) {

                cout << “*”; // Print star at borders and top row

            } else {

                cout << ” “; // Print space inside the pyramid

            }

        }

        cout << endl;

    }

    return 0;

}

Explanation:

  1. Header and Namespace: The program includes <iostream> for input/output operations and uses the std
  2. Input: The user is prompted to enter the number of rows for the pyramid. This number determines the height of the inverted pyramid.
  3. Loop for Rows:
    • Outer Loop: Runs from the specified number of rows down to 1, handling each row of the pyramid.
    • First Inner Loop: Manages the spaces before the stars begin on each row. These spaces increase as you move to lower rows (which are visually higher in the inverted pyramid).
    • Second Inner Loop: Manages the printing of stars and hollow spaces. Stars are printed at the first and last positions (j == 1 or j == 2 * i – 1) and across the entire top row (i == rows). The rest is filled with spaces.

Program Output:

Assuming the user inputs 5 for the number of rows, the output will be:

*********

*     *

*   *

* *

*

This program uses a combination of loops and conditional logic to create an appealing pattern. Such exercises are useful for practicing how to manipulate output based on the location within the loops, a key skill in developing more complex algorithms and applications in C++.

C++ Program to Print Full Diamond Shape Pyramid

Creating a full diamond shape pyramid using characters in C++ involves using nested loops to print spaces and characters in a specific format. This pattern is visually appealing and commonly used to practice loop constructs. The diamond pattern consists of two parts: an upper pyramid and an inverted lower pyramid.

Example: Full Diamond Shape Pyramid Using Stars (*)

This program will print a diamond shape pyramid made of stars (*). The user will input the number of rows for the upper pyramid. The lower pyramid will automatically be one row less than the upper to complete the diamond shape.

C++ Code:

#include <iostream>

using namespace std;

int main() {

    int rows;

    // User inputs the number of rows for the upper part of the diamond

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the upper part of the diamond

    for (int i = 1; i <= rows; ++i) {

        // Print leading spaces

        for (int j = rows – i; j > 0; –j) {

            cout << ” “;

        }

        // Print stars

        for (int j = 1; j <= 2 * i – 1; ++j) {

            cout << “*”;

        }

        cout << endl;

    }

    // Print the lower part of the diamond

    for (int i = rows – 1; i > 0; –i) {

        // Print leading spaces

        for (int j = 0; j < rows – i; ++j) {

            cout << ” “;

        }

        // Print stars

        for (int j = 1; j <= 2 * i – 1; ++j) {

            cout << “*”;

        }

        cout << endl;

    }

    return 0;

}

Explanation:

  1. Header and Namespace:

The program includes the <iostream> header for input and output operations and uses the std namespace.

  1. Input:

The user is prompted to enter the number of rows for the upper half of the diamond.

  1. Upper Diamond:

    • Outer Loop: Controls the rows of the upper pyramid.
    • First Inner Loop: Manages the spaces before the stars begin on each row. The spaces decrease as the row number increases.
    • Second Inner Loop: Manages the printing of stars. The number of stars on each row is 2 * i – 1, forming the pyramid shape.
  2. Lower Diamond:

    • Outer Loop: Controls the rows of the lower inverted pyramid.
    • First Inner Loop: Similar to the upper half but in reverse order for spaces.
    • Second Inner Loop: Handles the printing of stars, decreasing the count as the rows decrease to form the inverted pyramid.

Program Output:

If the user inputs 4 for the number of rows, the output will be:

*

***

*****

*******

*****

***

*

C++ Program to Print Continuous Character Pattern

Printing a continuous character pattern in C++ involves creating a visual arrangement where characters are printed in a sequence that continues across rows. A popular pattern is one where the characters change in each row continuously from the previous rows.

Example: Continuous Alphabet Pyramid Pattern

This pattern will feature a continuous increment in characters across each row, making each successive row start with the next character in the alphabet from where the last row ended.

  • C++ Code:

#include <iostream>

using namespace std;

int main() {

    int rows;

    char startChar = ‘A’;

    // User inputs the number of rows for the pattern

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the character pattern

    for (int i = 1; i <= rows; ++i) {       // Outer loop for each row

        for (int j = rows – i; j > 0; –j) { // Inner loop for leading spaces

            cout << ” “;

        }

        for (int k = 0; k < 2 * i – 1; ++k) { // Inner loop for characters

            cout << startChar;                // Print the current character

            startChar++;                      // Increment the character for the next print

            if (startChar > ‘Z’) startChar = ‘A’; // Reset to ‘A’ if past ‘Z’

        }

        cout << endl; // Move to the next line after each row is complete

    }

    return 0;

}

Explanation:

  • Header and Namespace:

The program starts by including the <iostream> header for input/output operations, and it uses the std

  • Input:

The user is asked to enter the number of rows for the pattern. This number defines the height of the pyramid.

  • Outer Loop:

It controls the number of rows in the pyramid. It runs from 1 up to rows.

  • First Inner Loop:

This loop manages the spaces before the characters start on each row. As the row number increases, the number of spaces decreases, ensuring the pyramid is centered.

  • Second Inner Loop:

This loop handles the printing of characters. The characters are printed continuously with each successive character increasing by one. It wraps around to ‘A’ after ‘Z’.

  • Character Management:

Characters are incremented with each print, and there is a wrap-around check to start again at ‘A’ if the character exceeds ‘Z’.

Program Output:

Assuming the user inputs 5 for the number of rows, the output will be:

    A

   BCD

  EFGHI

 JKLMNOP

QRSTUVWXY

This example elegantly demonstrates how to manage characters in a continuous pattern, utilizing nested loops, character increment, and simple condition checks to wrap around the alphabet. The program is flexible and can be easily adjusted for different patterns or different ranges of characters.

C++ Program to Print Character Pattern

Printing Character patterns is a classic programming exercise that helps beginners understand nested loops and control structures in C++. Here, I will provide a simple C++ program to print a character pattern, specifically a pyramid made of letters. This example will increment the letters alphabetically as it builds the pyramid.

Example: Alphabet Pyramid Pattern

This program will create a pyramid where each row contains the same character, starting from ‘A’ in the first row, ‘B’ in the second, and so on.

  • C++ Code:

#include <iostream>

using namespace std;

int main() {

    int rows;

    // User inputs the number of rows for the pattern

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the character pattern

    for (int i = 1; i <= rows; ++i) {        // Outer loop for each row

        for (int j = rows – i; j > 0; –j) { // Inner loop for leading spaces

            cout << ” “;

        }

        for (int k = 1; k <= 2 * i – 1; ++k) { // Inner loop for characters

            cout << char(‘A’ + i – 1);         // Print character corresponding to row number

        }

        cout << endl;  // Move to the next line after each row is complete

    }

    return 0;

}

 

Explanation:

  • Header and Namespace:

The program includes <iostream> for input and output operations and uses the std namespace.

  • Input:

The program prompts the user to enter the number of rows. This value determines the height of the pyramid.

  • Outer Loop:

This loop runs from 1 to rows, where each iteration represents a row in the pyramid.

  • First Inner Loop:

This loop prints spaces before the characters on each row. The number of spaces decreases as the row number increases, creating the left alignment of the pyramid.

  • Second Inner Loop:

This loop prints characters. It calculates the number of characters to print based on the row number (2 * i – 1). All characters in a single row are the same and correspond to their row number in the alphabet (char(‘A’ + i – 1)).

  • Output:

Each character is printed next to each other without spaces in this version, and each row ends with a newline.

Program Output:

If the user enters 4 for the number of rows, the output will be:

     A

   BBB

  CCCCC

DDDDDDD

This program is a straightforward demonstration of using nested loops and ASCII arithmetic to create patterns in C++. Adjusting the loops and the logic for character calculation allows creation of various other patterns and designs.

C++ Program to Print Number Pattern without Reassigning

Printing a number pattern in C++ often involves creating nested loops. A common challenge or limitation for some tasks might be not to reassign variables within the loops.

  • Example: Ascending Number Pattern

Let’s create a simple program that prints an ascending number pattern, where each row starts from 1 and goes up to the number equal to the row number.

C++ Code:

 

#include <iostream>

using namespace std;

int main() {

    int rows;

    // User inputs the number of rows for the pattern

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the number pattern

    for (int i = 1; i <= rows; ++i) {  // Outer loop for each row

        for (int j = 1; j <= i; ++j) { // Inner loop for each number in the row

            cout << j << ” “;  // Print each number followed by a space

        }

        cout << endl;  // Move to the next line after each row is complete

    }

    return 0;

}

 

Explanation:

  • Header and Namespace:

The program includes the <iostream> header for input/output operations and uses the std namespace.

  • Input:

The program prompts the user to enter the number of rows for the pattern. This determines how tall the pattern will be.

  • Outer Loop:

This loop controls the rows of the pattern. It runs from 1 to rows. The variable i indicates the current row number and is used to determine how many numbers to print on that row.

  • Inner Loop:

Inside each row, this loop prints numbers starting from 1 up to the row number (i). The variable j represents the current number being printed.

  • Output:

Each number is followed by a space (cout << j << ” “;), and each row ends with a newline (cout << endl;).

Program Output:

If the user enters 5 for the number of rows, the output will be:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

This program achieves the task of printing a simple number pattern without reassigning any variables within the loop bodies, sticking to the initial assignments. Each variable in the loops (i and j) is assigned only once per loop cycle and used consistently within its scope. This pattern is scalable and can be adjusted or expanded to include different sequences or formats by modifying the initial values and conditions of the loops.

C++ Program to Print Triangle Pattern

To create a triangle pattern in C++, you can use nested loops to manipulate the placement of characters (like asterisks *) on each line. The type of triangle you wish to print can vary in shape and size; examples include right-angled triangles, equilateral triangles, or inverted triangles.

Here, I’ll provide a simple C++ program to print a right-angled triangle using asterisks (*). This triangle aligns along the left side, making it straightforward to understand and implement.

Program to Print a Right-Angled Triangle Pattern

#include <iostream>

using namespace std;

int main() {

    int rows;

    cout << “Enter the number of rows for the triangle: “;

    cin >> rows;

    // Loop through each row

    for(int i = 1; i <= rows; i++) {

        // Print stars in each column

        for(int j = 1; j <= i; j++) {

            cout << “* “;

        }

        // Move to the next line after each row is printed

        cout << endl;

    }

    return 0;

}

 

Breakdown of the Program

  1. Include Header and Namespace:

The program begins by including the <iostream> header for input and output operations and uses the std namespace.

  1. Input Number of Rows:

The user is prompted to enter the number of rows for the triangle, which determines its height.

  1. Outer Loop:

This loop iterates through each row, from 1 to rows. Each iteration corresponds to a row in the triangle.

  1. Inner Loop:

Inside the outer loop, another loop runs from 1 to the current row number (i). This ensures that the number of asterisks printed increases by one with each new row, forming the right-angled triangle shape.

  1. Printing New Line:

After each row is printed, a newline character is added (cout << endl;) to move to the next row.

Running the Program

When you run this program, it might look something like this if you input 5 for the number of rows:

*

* *

* * *

* * * *

* * * * *

Each row increases the number of stars printed, and the pattern visually forms a right-angled triangle aligned to the left. This example can be modified to create other types of triangle patterns by adjusting the inner loop and possibly adding additional loops for spaces, depending on whether you want the triangle to be centered or inverted.

C++ Program to Print Inverted Pyramid

Creating an inverted pyramid in C++ is a straightforward task that involves careful manipulation of loops to print elements in a specific format. This task demonstrates the use of nested loops and control structures to create a pattern that resembles an upside-down pyramid, typically constructed using asterisks (*).

Understanding the Structure

An inverted pyramid consists of rows of elements that start with a relatively large number on the top row and decrease in each subsequent row. For a traditional pyramid pattern printed with stars, an inverted version would start with the widest row of stars at the top and reduce the number of stars by a fixed amount in each subsequent row until it reaches the smallest row at the bottom.

Program Outline

The C++ program will perform the following tasks:

  1. Prompt the user to input the number of rows for the inverted pyramid.
  2. Use a loop to iterate from the number of rows down to one.
  3. In each iteration, print the required number of stars and spaces to create the inverted pyramid appearance.

Detailed C++ Program

#include <iostream>

using namespace std;

int main() {

    int rows;

    cout << “Enter the number of rows for the inverted pyramid: “;

    cin >> rows;

    // Outer loop decrements to reduce the number of stars in each row

    for (int i = rows; i > 0; i–) {

        // Print leading spaces to center the stars

        for (int space = 0; space < rows – i; space++) {

            cout << ” “;

        }

        // Print stars: number of stars decreases as the loop decrements

        for (int star = 1; star <= 2 * i – 1; star++) {

            cout << “*”;

        }

        cout << endl;  // Move to the next line after each row is printed

    }

    return 0;

}

 

Breakdown of the Program

  1. User Input for Rows:

The program prompts the user to specify the number of rows, which will determine the base width of the pyramid.

  1. Outer Loop:

This loop starts from the number of rows and decrements down to 1. Each loop iteration corresponds to a row in the inverted pyramid, starting from the widest part.

  1. Inner Loop for Spaces:

Before printing stars, this loop prints spaces to ensure the stars are centered appropriately. The number of spaces is fewer for upper rows and increases as the pyramid narrows (as we move upwards in terms of traditional pyramid structure).

  1. Inner Loop for Stars:

This loop is responsible for printing the stars. The number of stars starts from the maximum (2 * rows – 1 for the first row) and decreases as we move to subsequent rows. The formula 2 * i – 1 ensures the correct number of stars is printed, which decrements symmetrically.

  1. Line Break After Each Row:

A newline (endl) is printed at the end of each row to start a new line for the subsequent row.

C++ Program to Print Simple Full Pyramid Pattern

Printing a full pyramid pattern using C++ involves using nested loops to properly align stars (or any other characters) in a symmetrical pyramid shape. A full pyramid pattern is centered, and each row contains an increasing number of stars based on the row number.

Here’s a C++ program that demonstrates how to print a simple full pyramid pattern:

#include <iostream>

using namespace std;

int main() {

    int rows;

    cout << “Enter the number of rows for the pyramid: “;

    cin >> rows;

    for (int i = 1; i <= rows; i++) {

        // Print spaces for alignment to the center

        for (int space = 1; space <= rows – i; space++) {

            cout << ” “;

        }

        // Print stars: there are 2*i – 1 stars in the ith row

        for (int star = 1; star <= 2 * i – 1; star++) {

            cout << “*”;

        }

        cout << endl; // Move to the next line after each row is printed

    }

    return 0;

}

Explanation of the Program:

  • User Input for Rows:

The user is prompted to enter the number of rows (rows) for the pyramid. This number determines the height of the pyramid.

  • Outer Loop for Rows:

This loop iterates from 1 up to rows. Each iteration of this loop represents one row of the pyramid.

  • Inner Loop for Printing Spaces:

Before printing any stars, this loop prints spaces to ensure that the pyramid is centered. The number of spaces decreases as the row number increases. For each row i, it prints rows – i spaces.

  • Inner Loop for Printing Stars:

This loop prints the stars needed for each row. In the ith row, there are 2*i – 1 stars. This ensures that the pyramid has a symmetrical appearance with a wider base and a single star at the top.

  • Line Break After Each Row:

After printing the required number of stars, a newline (endl) is used to move to the next line, ensuring that each level of the pyramid starts on a new line.

C++ Program to Print Left Half Pyramid Pattern

Creating a left half pyramid pattern in C++ involves using nested loops to align a series of asterisks (*) into a pyramid shape, aligning the right side of the pyramid against the left margin of your output console. This pattern is easier to generate because it does not require additional spaces for alignment, unlike the right half pyramid.

Here’s how you can write a C++ program to print a left half pyramid pattern:

#include <iostream>

using namespace std;

int main() {

    int rows;

    cout << “Enter the number of rows for the left half pyramid: “;

    cin >> rows;

    for (int i = 1; i <= rows; i++) {

        // Print stars: number of stars on each row equals the row number

        for (int j = 1; j <= i; j++) {

            cout << “* “;

        }

        cout << endl; // Move to the next line after each row is printed

    }

    return 0;

}

 

Explanation of the Program:

  1. User Input for Rows:

The program prompts the user to enter the number of rows for the pyramid. This value dictates how tall the pyramid will be.

  1. Outer Loop:

This loop runs from 1 to the entered number of rows. Each iteration of this loop represents a single row of the pyramid.

  1. Inner Loop for Printing Stars:

Within the outer loop, this inner loop prints a series of stars (*). The number of stars printed in each row corresponds to the current row number (i). For example, the first row will have one star, the second row two stars, and so on.

  1. Line Break After Each Row:

After printing the stars for a row, a newline is output using cout << endl; to move the cursor to the start of the next line. This ensures that each level of the pyramid appears on a new line.

error: Content is protected !!