C++ Program to Print Continuous Character Pattern

29/04/2024 0 By indiafreenotes

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.