codescracker
c++

C++ Program to Print Star and Pyramid Patterns



« Previous Program Next Program »


In this article, you will learn and get code to print many types of patterns using stars (*), numbers and alphabets to create Pyramid, triangle etc. like shapes. Here are the list of pattern programs in C++:

And a lot more programs are available in this article. Let's start with very first one, that is, printing of a half-pyramid pattern using stars (*).

Print Half-Pyramid Pattern of Stars (*)

This is the first program of this article that prints half-pyramid pattern of stars (*).

// C++ Print Half-Pyramid Pattern of Stars
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j;
    for(i=0; i<6; i++)
    {
        for(j=0; j<=i; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE, produces pattern of stars that looks like half-pyramid as shown in the snapshot given below:

C++ program print patterns

The dry run of for loop of this program goes like:

To print patterns in C++ programming, you have to use two for loop, the outer for loop and the inner for loop. The outer for loop is responsible for rows and the inner for loop is responsible for columns.

Print Inverted Half-Pyramid Pattern using Stars (*)

This C++ program prints an inverted half-pyramid pattern using stars (*).

// C++ Print Inverted Half-Pyramid Pattern using Stars
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j;
    for(i=0; i<6; i++)
    {
        for(j=i; j<6; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program produces another pattern of stars that looks like an inverted half-pyramid of stars as shown in the sample output given below:

print pattern in C++ Programming

Print Full Pyramid Pattern of Stars (*)

Following C++ program will print a full pyramid pattern of stars (*).

// C++ Print Full Pyramid of Stars
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, space, k=0;
    for(i=1; i<=6; i++)
    {
        for(space=1; space<=(6-i); space++)
            cout<<"  ";
        while(k!=(2*i-1))
        {
            cout<<"* ";
            k++;
        }
        k=0;
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program produces full pyramid pattern of stars as shown in the sample output given below:

C++ program print star pattern

You can also print full Pyramid pattern of stars using following code, instead of previous one:

// C++ Print Full Pyramid Pattern of Stars
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, space, j;
    for(i=1; i<=6; i++)
    {
        for(space=6; space>i; space--)
            cout<<" ";
        for(j=0; j<i; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

With this C++ code, the pyramid looks like:

pyramid pattern of stars c++

Print Inverted Full Pyramid Pattern using Stars (*)

This C++ program prints an inverted full Pyramid pattern using stars (*):

// C++ Print Inverted Full Pyramid Pattern using Stars
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, space, j;
    for(i=1; i<=6; i++)
    {
        for(space=1; space<i; space++)
            cout<<" ";
        for(j=i; j<=6; j++)
        {
            cout<<"* ";
        }
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program produces the pattern of stars that looks like an inverted full Pyramid as shown in the snapshot given below:

c++ program to print patterns

Print Pattern of Stars

Based on previous four programs, we've created a menu-driven program that receives an input from user as choice, that what user wants to print. That is, what pattern of stars, user wants to print:

// C++ Program to Print Pattern of Stars
// ----codescracker.com----

#include<iostream>
using namespace std;
void halfPyramid();
void invertedHalfPyramid();
void fullPyramid();
void invertedFullPyramid();
int main()
{
    int ch;
    do
    {
        cout<<"1. Print Half Pyramid of Stars\n";
        cout<<"2. Print Inverted Half Pyramid of Stars\n";
        cout<<"3. Print Full Pyramid of Stars\n";
        cout<<"4. Print Inverted Full Pyramid of Stars\n";
        cout<<"5. Exit\n";
        cout<<"Enter the Choice: ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                halfPyramid();
                break;
            case 2:
                invertedHalfPyramid();
                break;
            case 3:
                fullPyramid();
                break;
            case 4:
                invertedFullPyramid();
                break;
            case 5:
                return 0;
            default:
                cout<<"\nWrong Choice!";
                break;
        }
    }while(ch>=1 && ch<=4);
    cout<<endl;
    return 0;
}
void halfPyramid()
{
    int i, j;
    for(i=0; i<6; i++)
    {
        for(j=0; j<=i; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
}
void invertedHalfPyramid()
{
    int i, j;
    for(i=0; i<6; i++)
    {
        for(j=i; j<6; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
}
void fullPyramid()
{
    int i, space, j;
    for(i=1; i<=6; i++)
    {
        for(space=1; space<=(6-i); space++)
            cout<<" ";
        for(j=1; j<=i; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
}
void invertedFullPyramid()
{
    int i, space, j;
    for(i=1; i<=6; i++)
    {
        for(space=1; space<i; space++)
            cout<<" ";
        for(j=i; j<=6; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
}

Here is its initial output:

c++ program print pattern of numbers and stars

Now supply the input say 1 as your choice, if you want to print half pyramid of stars, here is the sample output after supplying 1 as input and pressing ENTER key:

c++ print pyramid of stars

As you can see, the half pyramid gets printed, and the menu again gets displayed. Now you can operate further, otherwise if you want to skip or exit, then type 5 or enter 5 and exit from the program of printing pattern of stars in four ways. Here is another snapshot of sample run. This snapshot shows the use of all the choices:

c++ print pattern of stars

C++ Print Star (*) Pattern

This program prints the pattern of stars (*) in a way that, one star in first row, three star in second row, five star in third row and so on upto sixth row.

// C++ Print Star (*) Pattern
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, k=1;
    for(i=0; i<6; i++)
    {
        for(j=0; j<k; j++)
            cout<<"* ";
        k=k+2;
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

When the above C++ program is compile and executed, it will produce the following output:

print star pattern in C++ Programming

Here is another program that prints the same as of previous program, but in an inverted way:

// C++ Program to Print Star Pattern
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, space=20, k=1;
    for(i=0; i<6; i++)
    {
        for(j=0; j<space; j++)
            cout<<" ";
        space = space-4;
        for(j=0; j<k; j++)
            cout<<"* ";
        k = k+2;
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

The snapshot given below shows the sample output of this C++ program:

C++ program print pattern using looping

C++ Print Triangle Star Pattern

This C++ program prints star pattern that looks like triangle or half-pyramid, whatever you say.

// C++ Print Triangle Star Pattern
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, space=10;
    for(i=0; i<6; i++)
    {
        for(j=0; j<space; j++)
            cout<<" ";
        space = space-2;
        for(j=0; j<=i; j++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

Here is its sample output, prints pattern of stars that looks like a triangle or half-pyramid:

looping in C++ Programming

C++ Print Pattern of Numbers

This program prints pattern of numbers (natural numbers). This number pattern looks like a half-pyramid:

// C++ Print Pattern of Numbers
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, num=1;
    for(i=0; i<6; i++)
    {
        for(j=0; j<=i; j++)
        {
            cout<<num<<" ";
            num++;
        }
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program produces a pattern of numbers that can also be called as a half-pyramid of natural numbers as shown in the following snapshot:

cpp pattern programs

Here is another program that does the same job as of previous program. The only difference is, this program starts with 1 at every row instead of natural numbers from first row first column to last row last column:

// C++ Print Pattern of Numbers
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, num;
    for(i=0; i<6; i++)
    {
        num=1;
        for(j=0; j<=i; j++)
        {
            cout<<num<<" ";
            num++;
        }
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

The snapshot given below shows the sample output of this program:

cpp pattern programs examples

C++ Print Pattern of Alphabets

This C++ program prints pattern of alphabets. That is, a half-pyramid using continuous alphabet characters starting from 'A' gets printed using following program:

// C++ Print Pattern of Alphabets
// ----codescracker.com-----

#include<iostream>
using namespace std;
int main()
{
    int i, j;
    char ch='A';
    for(i=0; i<6; i++)
    {
        for(j=0; j<=i; j++)
        {
            cout<<ch<<" ";
            ch++;
        }
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

Here is the sample output of this C++ program to print pattern of alphabets:

pattern programs in cpp

Here is another program that starts with new alphabet character in each row and prints in repeated order for all columns:

// C++ Print Pattern of Alphabet Characters
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j;
    char ch='A';
    for(i=0; i<6; i++)
    {
        for(j=0; j<=i; j++)
        {
            cout<<ch<<" ";
        }
        ch++;
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program produces the following output:

pattern program in c++

C++ Print Pyramid Pattern of Stars in Given Size

This program receives the input from user as size of Pyramid to print Pyramid pattern using stars (*) of the given size.

// C++ Print Pyramid of Given Size
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, space, rowSize, k=0;
    cout<<"Enter the Number of Rows: ";
    cin>>rowSize;
    cout<<"\nPyramid of "<<rowSize<<" Rows or Lines:\n";
    for(i=1; i<=rowSize; i++)
    {
        for(space=1; space<=(rowSize-i); space++)
            cout<<"  ";
        while(k!=(2*i-1))
        {
            cout<<"* ";
            k++;
        }
        k=0;
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This is the initial output of above program's sample run:

C++ program print star pyramid

Now supply the input say 6 as number of rows or lines to print pyramid pattern that are of 6 lines as shown in the snapshot given below:

cpp star

Here is another sample run with user input, 10:

c++ pattern program

Same Program in Other Languages


« Previous Program Next Program »