C++ Program:

- To print sideway triangle pattern using numbers

Example:

User Input: 3

Output:

1
1 2
1 2 3
1 2
1
#include <iostream>
using namespace std;

int main() {
  int columns,trows,k=1,num;
  cout<<"Enter number of columns: ";
  cin>>columns;
  trows=2*columns-1;
  for(int i=1; i<=trows; i++) {
    num=1;
    for(int j=1; j<=k; j++) {
      cout<<num++<<" ";
    }
    if(i<columns)k++;
    else k--;
    cout<<endl;
  }
  return 0;
}

Output:

Enter number of columns: 4

1
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1
Program
pattern

Main Logic :

trows=2*columns-1;   // trows : total rows to print
k=1;  // Used to trigger decreasing order of number

for(int i=1; i<=trows; i++) {
  num=1;
  for(int j=1; j<=k; j++) {
    cout<<num++<<" ";
  }
  //check for position change at which triangle number value to decrease
  if(i<columns)k++;
  else k--;

  cout<<endl;
}
Concept

Coming Soon !

Quick
Tutorial

Quote

Sometimes it’s the smallest decisions that can change your life forever.