Programs for printing pyramid patterns in C++

link 0

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++)
    {
        // re assigning num
        num = 1;
        //  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 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++)
    {
        // without re assigning num
        // num = 1;
        //  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 

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar