Problems On Pattern In C++

Problems On Pattern In C++

Pattern 1: Half Pyramid Pattern

 *

* *

* * *

* * * *

* * * * *

#include<iostream>
using namespace std;

int main() {
    int rows;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}


Pattern 2: Inverted Half Pyramid Pattern
* * * * *
* * * *
* * *
* *
*
#include<iostream>
using namespace std;

int main() {
    int rows;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Pattern 3: Full Pyramid Pattern

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

Pattern 4: Inverted Full Pyramid Pattern
* * * * * * * * * * * * * * *
#include<iostream> using namespace std; int main() { int rows; cout << "Enter the number of rows: "; cin >> rows; for (int i = rows; i >= 1; i--) { for (int space = 1; space <= rows - i; space++) { cout << " "; } for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; } return 0; }

Pattern 5: Number Pyramid Pattern
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
#include<iostream> using namespace std; int main() { int rows; cout << "Enter the number of rows: "; cin >> rows; for (int i = 1; i <= rows; i++) { for (int space = 1; space <= rows - i; space++) { cout << " "; } for (int j = 1; j <= i; j++) { cout << j << " "; } cout << endl; } return 0; }

Pattern 6:Pattern Diamond
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
#include<iostream> using namespace std; int main() { int rows; cout << "Enter the number of rows: "; cin >> rows; // Upper Half of Diamond for (int i = 1; i <= rows; i++) { for (int space = 1; space <= rows - i; space++) { cout << " "; } for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; } // Lower Half of Diamond for (int i = rows - 1; i >= 1; i--) { for (int space = 1; space <= rows - i; space++) { cout << " "; } for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; } return 0; }

Pattern 8: Hollow Diamond Pattern
* * * * * * * * * * * * * * * *
#include<iostream> using namespace std; int main() { int rows; cout << "Enter the number of rows: "; cin >> rows; // Upper Half of Hollow Diamond for (int i = 1; i <= rows; i++) { for (int space = 1; space <= rows - i; space++) { cout << " "; } for (int j = 1; j <= 2 * i - 1; j++) { if (j == 1 || j == 2 * i - 1) { cout << "*"; } else { cout << " "; } } cout << endl; } // Lower Half of Hollow Diamond for (int i = rows - 1; i >= 1; i--) { for (int space = 1; space <= rows - i; space++) { cout << " "; } for (int j = 1; j <= 2 * i - 1; j++) { if (j == 1 || j == 2 * i - 1) { cout << "*"; } else { cout << " "; } } cout << endl; } return 0;
}

If you are reading this it means you have solved all the problems . By solving these problems you can get a basic to advanced knowledge of patterns in c++. So till know Happy Coding.

Pattern 9: Butterfly Patter

#include<iostream> using namespace std; int main(){ int row; cout<<"Enter no of rows"<<endl; cin>>row; int space=2*row; //Upper part of butterfly for(int i=1;i<=(row+1);i++){ for(int j=1;j<=i;j++){ cout<<"*"; }
for(int k=1;k<=space;k++){ cout<<" "; }
space=space-2; for(int m=1;m<=i;m++){ cout<<"*"; }
cout<<endl; }
//lower part for(int i=1;i<=row;i++){ for(int j=1;j<=row+1-i;j++){ cout<<"*"; }
for(int j=1;j<=2*i;j++){ cout<<" "; }
for(int m=1;m<=row+1-i;m++){ cout<<"*"; }
cout<<endl; } }
Enter no of rows 6 * * ** ** *** *** **** **** ***** ***** ****** ****** ************** ****** ****** ***** ***** **** **** *** *** ** ** * *

Comments