
Part-3: special triangles problems
In this article, we are going to discuss some of the famous triangles patterns which are generally asked in college papers or contests. Try to remember the concept of these triangles and you can easily make their pattern with the help of code.
Topics Covered
- Floyd’s Triangle pattern problem
- Palindromic Triangle pattern problem
- Pascal’s Triangle pattern problem
- Binary Triangle (0-1) pattern problem
1. Floyd’s Triangle pattern problem
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Floyd’s triangle is defined by filling the rows of the triangle with consecutive numbers. The total number of elements in a row is equal to the row number i.e. print one number in 1st row, two numbers in 2nd row and so on. Let’s try to do this with code
CODE:
#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"Enter the height of triangle "<<endl;
cin>>h;
int initial=1;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=i; j++)
{
cout<< initial <<" ";
initial++;
}
cout<<endl;
}
return 0;
}
OUTPUT:
2. Palindromic Triangle pattern problem
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
palindrome pattern is a word, number, phrase, or other sequence of characters or numbers which reads the same backward as forward like eye, 23432, etc. In the same way a palindromic triangle is a triangle pattern in which every row follows a palindrome pattern and when we look at the pattern the total number of elements in row is equal to 2*row number – 1. so lets apply this to code
CODE:
#include<iostream>
using namespace std;
int main()
{
int h,j;
cout<<"Enter the height of triangle "<<endl;
cin>>h;
for(int i=1; i<=h; i++)
{
int k=i;
for(j=1;j<=(h-i);j++)
{
cout<<" ";
}
for(; j<=h;j++)
{
cout<<k<<"";
k--;
}
k=1;
for(; j<(h+i);j++)
{
k++;
cout<<k<<"";
}
for(; j<=(2*h-1);j++)
{
cout<<" ";
}
cout<<endl;
}
return 0;
}
OUTPUT:
3. Pascal’s Triangle pattern problem
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Pascal’s triangle is a triangular array of the binomial coefficients. In this triangle numbers are arranged in a order that lower row element is the sum of its adjacent upper element and 1st and last element of every row is 1. Let’s try this with code
CODE:
#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"Enter the height of triangle "<<endl;
cin>>h;
int num=1;
for(int i=0; i<h;i++)
{
for (int space=1; space<=h-i; space++)
{
cout<<" ";
}
for (int j=0; j<=i; j++)
{
if(j==0 || i==0)
{
num=1;
}
else
{
num=num*(i-j+1)/j;
}
cout << num<<" ";
}
cout<<endl;
}
return 0;
}
OUTPUT:
4. Binary Triangle (0-1) pattern problem
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
A binary triangle is formed from the alternate combination of 0 and 1. We use even and odd method whenever we want to apply binary in the code. Lets try this with code
CODE:
#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"Enter the height of triangle "<<endl;
cin>>h;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=i; j++)
{
if((i+j)%2==0)
cout<<" 1";
else cout<<" 0";
}
cout<<endl;
}
return 0;
}
OUTPUT:
These are some of the special triangles that we draw with the help of code. Try to make some more triangles yourself to practice.
In Part-5, we will discuss about some pattern of diamonds.