Tutorialspoint.dev

Programs for printing pyramid patterns in C++

This article is aimed at giving a C++ implementation for pattern printing.

  • Simple pyramid pattern
  • // C++ code to demonstrate star pattern
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void pypart(int n)
    {
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i<n; i++)
        {
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for(int j=0; j<=i; j++ )
            {
                // printing stars
                cout << "* ";
            }
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        pypart(n);
        return 0;
    }

    Output:

    * 
    * * 
    * * * 
    * * * * 
    * * * * * 
    

  • After 180 degree rotation
  • // C++ code to demonstrate star pattern
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void pypart2(int n)
    {
        // number of spaces
        int k = 2*n - 2;
      
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i<n; i++)
        {
      
            // inner loop to handle number spaces
            // values changing acc. to requirement
            for (int j=0; j<k; j++)
                cout <<" ";
      
            // decrementing k after each loop
            k = k - 2;
      
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for (int j=0; j<=i; j++ )
            {
                // printing stars
                cout << "* ";
            }
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        pypart2(n);
        return 0;
    }

    Output:



            * 
          * * 
        * * * 
      * * * * 
    * * * * * 
    

  • Printing Triangle
  • // C++ code to demonstrate star pattern
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void triangle(int n)
    {
        // number of spaces
        int k = 2*n - 2;
      
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i<n; i++)
        {
            // inner loop to handle number spaces
            // values changing acc. to requirement
            for (int j=0; j<k; j++)
                cout <<" ";
      
            // decrementing k after each loop
            k = k - 1;
      
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for (int j=0; j<=i; j++ )
            {
                // printing stars
                cout << "* ";
            }
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        triangle(n);
        return 0;
    }

    Output:

        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
    

  • Number Pattern
  • // C++ code to demonstrate printing pattern of numbers
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void numpat(int n)
    {
        // initializing starting number
        int num = 1;
      
        // outer loop to handle number of rows
        // n in this case
        for (int i=0; i<n; i++)
        {
      
            // inner loop to handle number of columns
            // values changing acc. to outer loop
            for (int j=0; j<=i; j++ )
                cout << num << " ";
      
            // incrementing number at each column
            num = num + 1;
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        numpat(n);
        return 0;
    }

    Output:

    1 
    2 2 
    3 3 3 
    4 4 4 4 
    5 5 5 5 5 
    

  • Numbers without re assigning
  • // C++ code to demonstrate  printing pattern of numbers
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void numpat(int n)
    {
        // initialising starting number
        int num = 1;
      
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i<n; i++)
        {
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for (int j=0; j<=i; j++ )
            {
                // printing number
                cout << num << " ";
      
                // incrementing number at each column
                num = num + 1;
            }
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        numpat(n);
        return 0;
    }

    Output:

    1 
    2 3 
    4 5 6 
    7 8 9 10 
    11 12 13 14 15 
    



  • Character Pattern
  • // C++ code to demonstrate printing pattern of alphabets
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void alphapat(int n)
    {
        // initializing value corresponding to 'A'
        // ASCII value
        int num = 65;
      
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i<n; i++)
        {
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for (int j=0; j<=i; j++ )
            {
                // explicitly converting to char
                char ch = char(num);
      
                // printing char value
                cout << ch << " ";
            }
      
            // incrementing number
            num = num + 1;
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        alphapat(n);
        return 0;
    }

    Output:

    A 
    B B 
    C C C 
    D D D D 
    E E E E E 
    

  • Continuous Character pattern
  • // C++ code to demonstrate printing pattern of alphabets
    #include <iostream>
    using namespace std;
      
    // Function to demonstrate printing pattern
    void contalpha(int n)
    {
        // initializing value corresponding to 'A'
        // ASCII value
        int num = 65;
      
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i<n; i++)
        {
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for (int j=0; j<=i; j++ )
            {
                // explicitely converting to char
                char ch = char(num);
      
                // printing char value
                cout << ch << " ";
      
                // incrementing number at each column
                num = num + 1;
            }
      
            // ending line after each row
            cout << endl;
        }
    }
      
    // Driver Function
    int main()
    {
        int n = 5;
        contalpha(n);
        return 0;
    }

    Output:

    A 
    B C 
    D E F 
    G H I J 
    K L M N O 
    

    Printing patterns in python language are discussed in the article below :

    Programs for printing pyramid patterns in Python

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



    This article is attributed to GeeksforGeeks.org

    leave a comment

    code

    0 Comments

    load comments

    Subscribe to Our Newsletter