Print asterisk patterns - C++ Programs

1. Write a C++ program to display following pattern:

pattern 1

Answer:

#include<iostream>
using namespace std;
int main()
{
        int i, j, cnt;
        cout<<"\n Enter Number of Rows : ";
        cin>>cnt;
        cout<<"\n";
        for(i=0; i<cnt; i++)
        {
                for(j=cnt; j>i; j--)
                {
                        cout<<" * ";
                }
                cout<<"\n";
        }
        return 0;
}


Output:

pattern 1

2. Write a C++ program to draw the following pattern:

pattern 2

Answer:

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


Output:

pattern 2

3. Write a C++ program to draw the following pattern:

pattern 3

Answer:

#include <iostream>
using namespace std;
int main()
{
        int rows, i, j, space;
        cout<<"Enter Number of Rows : ";
        cin>>rows;
        cout<<"\n";
        for(i=rows; i>=1; --i)
        {
                for(space=0; space<rows-i; ++space)
                        cout<<"  ";
                for(j=i; j<=2*i-1; ++j)
                        cout<<"* ";
                for(j=0; j<i-1; ++j)
                        cout<<"* ";
                cout<<endl;
        }
        return 0;
}


Output:

pattern 3