Important Pattern Questions
Pattern 1 - Solid Rectangle
#include < iostream>
using namespace std ;
int main () {
int row, col;
cin >> row >> col;
for ( int i = 1 ; i <= row; i++ ) {
for ( int j = 1 ; j <= col; j++ ) {
cout << " *" ;
}
cout << endl;
}
return 0 ;
}
Pattern 2 - Hollow Rectangle
#include < iostream>
using namespace std ;
int main () {
int row, col;
cin >> row >> col;
for ( int i = 1 ; i <= row; i++ ) {
for ( int j = 1 ; j <= col; j++ ) {
i == 1 || i == row || j == 1 || j == col ? cout << " *" : cout << " " ;
}
cout << endl;
}
return 0 ;
}
Pattern 3 - Inverted Half Pyramid
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = n; i >= 1 ; i-- ) {
for ( int j = 1 ; j <= i; j++ ) {
cout << " *" ;
}
cout << endl;
}
return 0 ;
}
Pattern 4 - Half Pyramid After 180deg Rotation
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= n; j++ ) {
j <= n - i ? cout << " " : cout << " *" ;
}
cout << endl;
}
return 0 ;
}
Pattern 5 - Half Pyramid Using Numbers
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
cout << i;
}
cout << endl;
}
return 0 ;
}
Pattern 6 - Floyd's Triangle
1
23
456
78910
1112131415
#include < iostream>
using namespace std ;
int main () {
int n, k = 1 ;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
cout << k;
k++;
}
cout << endl;
}
return 0 ;
}
* *
** **
*** ***
********
********
*** ***
** **
* *
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= 2 *n; j++ ) {
j <= i || j > 2 *n - i ? cout << " *" : cout << " " ;
}
cout << endl;
}
for ( int i = n; i >= 1 ; i-- ) {
for ( int j = 1 ; j <= 2 *n; j++ ) {
j <= i || j > 2 *n - i ? cout << " *" : cout << " " ;
}
cout << endl;
}
return 0 ;
}
Pattern 8 - Inverted Pattern
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = n; i >= 1 ; i--) {
for ( int j = 1 ; j <= i; j++ ) {
cout << j;
}
cout << endl;
}
return 0 ;
}
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
(i+j) % 2 == 0 ? cout << 1 : cout << 0 ;
}
cout << endl;
}
return 0 ;
}
*****
*****
*****
*****
*****
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = n; i >= 1 ; i-- ) {
for ( int j = 1 ; j <= n + i - 1 ; j++ ) {
j <= i-1 ? cout << " " : cout << " *" ;
}
cout << endl;
}
return 0 ;
}
Pattern 11 - Number Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= n-i; j++ ) cout << " " ;
for ( int j = 1 ; j <= i; j++ ) cout << j << " " ;
cout << endl;
}
return 0 ;
}
Pattern 12 - Palindrome Pattern
1
212
32123
4321234
543212345
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= n - i; j++ ) cout << " " ;
for ( int j = i; j >= 1 ; j-- ) cout << j;
for ( int j = 2 ; j <= i; j++ ) cout << j;
cout << endl;
}
return 0 ;
}
Pattern 13 - Star Pattern
*
***
*****
*******
*********
*********
*******
*****
***
*
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= n; j++ ) {
j <= n - i ? cout << " " : cout << " *" ;
}
for ( int j = i - 1 ; j >= 1 ; j-- ) cout << " *" ;
cout << endl;
}
for ( int i = n; i >= 1 ; i-- ) {
for ( int j = 1 ; j <= n; j++ ) {
j <= n - i ? cout << " " : cout << " *" ;
}
for ( int j = i - 1 ; j >= 1 ; j-- ) cout << " *" ;
cout << endl;
}
return 0 ;
}
Pattern 14 - Zig-Zag Pattern
#include < iostream>
using namespace std ;
int main () {
int n;
cin >> n;
for ( int i = 1 ; i <= 3 ; i++ ) {
for ( int j = 1 ; j <= n; j++ ) {
( (i+j) % 4 == 0 ) || ( i==2 && j%4 == 0 ) ? cout << " *" : cout << " " ;
}
cout << endl;
}
}
Pattern 15 - Pascal's Triangle
#include < iostream>
using namespace std ;
int fact (int n) {
if ( n == 0 || n == 1 ) return 1 ;
return n * fact (n - 1 );
}
int nCr (int n, int r) {
return fact (n) / ( fact (n-r) * fact (r) );
}
int main () {
int n;
cin >> n;
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j <= i; j++ ) {
cout << nCr (i, j);
}
cout << endl;
}
return 0 ;
}