Saturday, 7 July 2018

Pyramid patterns

oid pypart(int n)
{
    // outer loop to handle number of rows
    //  n in this case
    for (int i=0; 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



  • // Function to demonstrate printing pattern
    void pypart2(int n)
    {
        // number of spaces
        int k = n;
     
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i
        {
     
            // inner loop to handle number spaces
            // values changing acc. to requirement
            for (int j=0; j
                cout <<" ";
     
            // decrementing k after each loop
            k--;
     
            //  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:
            * 
          * * 
        * * * 
      * * * * 
    * * * * * 





    No comments:

    Post a Comment