Patterns in C++

#include <iostream>
using namespace std;
int main()
{
    //normal pattern:
    int n;
    cout << "Enter the number of entries : ";
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < i + 1; j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
cout<<"----------------------------------"<<endl;
//reverse:
    for (int i = n; i > 0; i--)
    {
        for (int j = i; j > 0; j--)
        {
            cout<<"*";
        }
        cout<<endl;
    }
cout<<"----------------------------------"<<endl;
// staircase:
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < n-i; j++){
            cout<<" ";
        }
        for(int k = 0; k < i; k++){
            cout<<"#";
        }
        cout<<endl;
    }
cout<<"----------------------------------"<<endl;
//ABCD Pattern:
char letters[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for(int i =0; i < n; i++){
        for(int j=0; j< i+1;j++){
            cout<<letters[i];
        }
        cout<<endl;
    }
cout<<"----------------------------------"<<endl;
// *      *
// **    **
// ***  ***
// ********
// --------
// ***  ***
// **    **
// *      *
/*
There are two bases: One is for upper pattern and the other is for lower pattern.
Three for loops, one is for endlines and the other is for * and the last is for spaces. The spaces are made keeping consideration of blanks left.
*/
    for(int i=1;i<n+1;i++){
        for(int j=1;j<=i*2;j++){
            cout<<"*";
            if(j == i ){            //after half make spaces
                for(int k =0;k < (2*- 2*i);k++){   
                    cout<<" ";       //till no.of blanks left                       
                }
            }
            else{
                continue;
            }
        }
        cout<<endl;
    }
    for(int i=n-1;i>=1;i--){                //i -> -1
        for(int j=2*i;j>=1;j--){
            cout<<"*";
            if(j == i+1){           //j -> +1
                for(int k =0;k < (2*- 2*i);k++){    
                    cout<<" ";                              
                }
            }
            else{
                continue;
            }
        }
        cout<<endl;
    }
    return 0;
}


Comments

Popular Posts