C++ Examples – C++ Program to print triangle of characters as below




C++ Program to print triangle of characters as below

In this lesson we will learn how to print the triangle of characters of alphabets as shown below.

A
B B
C C C
D D D D
E E E E E
F F F F F F

 

#include <iostream>
using namespace std;
int main()
{
  int i,j;
  char input,temp='A';
  cout<<"Enter uppercase character you want in triange at last row: ";
  cin>>input;
  for(i=1;i<=(input-'A'+1);++i)
  {
    for(j=1;j<=i;++j)
      cout<<temp<<" ";
    ++temp;
    cout<<endl;
  }
  return 0;
}

/*
Enter uppercase character you want in triange at last row: F
A
B B
C C C
D D D D
E E E E E
F F F F F F

*/

C++ Program to Print Alphabet pattern (Alphabet Triangle)

A
BC
DEF
GHIJ
KLMNO
PQRSTU
#include <iostream>
using namespace std;
int main()
{
  int i,j,n;
  char c;
  cout<<"Eneter the no of lines to be printed: ";
  cin>>n;
  c='A';
  for(i=0;i<n;i++)
  {
    for(j=0;j<=i;j++)
    {
      if(c=='Z')
        break;
      cout<<c;
      c++;
    }
    cout<<endl;
  }
  return 0;
}

/*
Eneter the no of lines to be printed: 6
A
BC
DEF
GHIJ
KLMNO
PQRSTU

*/

C++ Program to Print Alphabet pattern in reverse order  (Alphabet Triangle)

 ABCDEF
  GHIJK
   LMNO
    PQR
     ST
      U
#include <iostream>
using namespace std;
int main()
{
  int i,j,n,k;
  char c='A';
  cout<<"Enter the no of lines to be printed: ";
  cin>>n;
  for(i=0;i<=n;i++)
  {
    for(j=0;j<=i;j++)
    {
      cout<<" ";
    }
    for(k=n-i-1;k>=0;k--)
    {
      cout<<c;
      c++;
    }
    cout<<endl;
  }
  return 0;
}

/*
Enter the no of lines to be printed: 6
 ABCDEF
  GHIJK
   LMNO
    PQR
     ST
      U

*/

After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax

  • if it finds errors, it lists them
  • If there are no errors, it translates the C++ program into a program in machine language which you can execute

 

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*