C ++ Program Alphabet Triangle Pattern

By | March 5, 2020

C Plus Plus Program Alphabet Triangle Pattern

Task: Write down a C++ Program to display Alphabet Triangle Pattern. The user will enter the height of triangle. Then the program will show a right angle triangle of alphabets in Capital letters.

C++ Program Alphabet Triangle Pattern

C++ Program Alphabet Triangle Pattern

How Alphbets Triangle Program Works

This program uses nested for loops to display the required output pattern. The program asks the user to enter height of the triangle. The user will enetr a whole number like 5 or 10 etc. The program will display the right angle triangle of capital letters according to the required height.

The first loop uses a loop control variable row. This row variable shows the lines of the output. Wheras the variable col represents a column to display characters.

This program uses a char variable intialized with ‘A’. It will print A in first row. And it will print two characters A and B in second row and so on.

The outer for loop in nested loops is used to control the number of lines or rows. Whereas the col varible is used to print the required number of alphabetics.

The Source Code of Alphabets Triangle Program

// C++ Program to display 
// Right Angle Triangle
// of Capital Alphabets A,B,C
#include<iostream>

using namespace std;

int main()
{

  /*
   A
   AB
   ABC
   ABCD
   ABCDE 
  */

  char row,col,ch;
  int height;
  cout<<"Enter the height of Triangle=";
  cin>>height;
  for(row=1;row<=height;row++)
  {
  	 ch='A';
	 for(col=1;col<=row;col++)
  	   {
		cout<<ch<<" ";
		ch++;
	   }
  	cout<<endl;
  	
  }
  return 0;

}

/*
OUTPUT
Enter the height of Triangle=10
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J

--------------------------------
Process exited after 28.16 seconds with return value 0
Press any key to continue . . .
*/

Output of Alphabet Triangle Program

OUTPUT

Enter the height of Triangle=10
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J
Another output from another sample run of this programEnter the height of Triangle=7
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G——————————–
Process exited after 5.711 seconds with return value 0
Press any key to continue . . .

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *