×

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:

C ++ Program: Alphabet Triangle and Number Triangle

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:

C ++ Program: Alphabet Triangle and Number Triangle

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:

C ++ Program: Alphabet Triangle and Number Triangle

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:

C ++ Program: Alphabet Triangle and Number Triangle

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:

C ++ Program: Alphabet Triangle and Number Triangle

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.


Related Topics

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.