Sunteți pe pagina 1din 12

GEEKSFORGEEKS

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 = 10;
    pypart2(n);
    return 0;
}

Output:

* *

* * *

* * * *

* * * * *

Printing Triangle

Method 1 Method 2

// 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:

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:

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:

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

This article is contributed by Manjeet Singh(S.Nandini). If you like GeeksforGeeks and


would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org.
See your article appearing on the GeeksforGeeks main page and help other Geeks.

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

Recommended Posts:

PHP programs for printing pyramid patterns


Programs for printing pyramid patterns using recursion
Programs for printing pyramid patterns in Java
Programs for printing pyramid patterns in Python
Programs to print Interesting Patterns
Programs to print Triangle and Diamond patterns using recursion
Print the following pyramid pattern
Program for volume of Pyramid
Program to print Even Odd Number Pyramid
Program to print pyramid pattern
Program to print pyramid pattern
Program to Print Pyramid Pattern using numbers
Program to print hollow pyramid and diamond pattern
Program to print a inverse pyramid character pattern
Print the pyramid pattern with given height and minimum number of stars

Improved By : Raghav12Singh

Article Tags : C++ School Programming pattern-printing

Practice Tags : pattern-printing CPP

Read Full Article


710-B, Advant Navis Business Park,
Sector-142, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise GBlog
Contests Videos
Subjective Questions

@geeksforgeeks, Some rights reserved

S-ar putea să vă placă și