# C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle

An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or random alphabetical elements grouped. It a pattern-based programming practice that is implemented using nested loops.

There is a certain number of ways of generating alphabetical triangles. Let us look at some of the most practiced ways through the coding examples below.

Example 1:

``` #include<bits/stdc++.h>
using namespace std;
int main()
{
int i, j, increment=1, count;
cout<<"Enter Rows : ";
cin>>count;
cout<<"\n";
char ch = 'A';
for(i=0; i<count; i++)
{
for(j=0; j<increment; j++)
{
cout<<ch<<" ";
ch++;
}
cout<<"\n";
increment = increment + 1;
}
return 0;
} ```

Output:

Explanation:

In the above code, we have declared a variable 'count' and 'increment'. Here the count variable takes the number of rows as input from the user and a loop iterates till the count. Another loop is used for columns that keep the alphabet incremented by 1 so that the letters consequently proceed one by one. The variable 'char' is initialized by 'A' so that the looping starts from A to the next word.

Note: The triangle above is also known as Floyd’s Triangle.

Example 2:

``` #include<bits/stdc++.h>
using namespace std;
int main()
{
char s[]="india";
int i,j;
for(i=0;s[i];i++)
{
for(j=0;j<=i;j++)
cout<<s[j];
cout<<"\n";
}
} ```

Output:

Explanation:

The above code is made interesting just to understand the flow of making an alphabetical triangle. In this example, we have initialized a character array known as a string and assigned the string as "India". Two nested loops are used to access the rows and columns and print them consequently in the next line keeping the previous row element the same.

Example:

``` #include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j;
int n=5;
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<((char)(i+64));
}
cout<<endl;
}
return 0;
} ```

Output:

Explanation:

Here, the same pattern is printed just in inverted or say upside-down order. This can be done just by doing minor changes in the above-explained codes. In this case, the loop is executed from the number itself and the term associated with it in the loop. The loop starts to iterate from E since and then the E is printed on the console. The process is then decreased 5 folds until the end condition is reached i.e. till A.

Note: There are various other approaches to do this yet this is the most practiced approach.

### Number Triangle:

A number triangle is exactly similar to the alphabetical triangle provided words are replaced just by numbers. The logic of coding is just the same. A number triangle is also generated using nested loops. To ace this, one must be ideally clear with the concept of nested loops.

Let us now look at coding examples for better understanding.

Example 1:

``` #include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cout<<"Please enter no. of rows: ";
cin>>num;
for(int i = 1;i <=num;i++)
{
for(int j = 1;j<=i;j++)
{
cout << j;
}
cout << "\n";
}
return 0;
} ```

Output:

Explanation:

Here we have used two loops that iterate for rows and columns respectively. As soon as the user enters the number of rows, the outer loop is executed which runs from 1 to the number entered. The loop then enters the column and prints the value on the console and later the number is incremented from 1 to 2 and so on.

Example 2:

``` #include<bits/stdc++.h>
using namespace std;
int main()
{
int number;
cout<<"Enter the size of triangle: ";
cin>>number;
for(int i = 1; i <=10;i++)
{
for(int j= number-i;j>= 1;j--)
{
cout << j;
}
cout<<"\n";
}
return 0;
} ```

Output:

Explanation:

In the above code, we have tried to invert the number triangle just to make the users understand the flow switches in the nested loops.

Here, we have initialized the outer loop from 1 to the number and the inner loop is further executed from the number minus the value from the outer loop. The inner loop is then decremented using the post-decrement operator. It gives the value in decreasing order in the next line till the value reaches its end.

There are still various other ways to print number triangles in C++ but we have discussed the most basic and easy to learn approaches for solving these pattern related problems.