C++ | Drawing a right angled triangle of numbers

Introduction

This program is to draw a right angled triangle of numbers in c++. You can also call this pyramid but it doesn’t look like real pyramid. However you can see my next posts for drawing a real pyramid. The program draw the triangle directly no need to input any thing. This program is extendable. Go enjoy the program. Lets begin…….

Program to draw a right angled triangle of numbers.


#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i,j,a=1;
//print triangle
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<a<<" ";
a++;
}
cout<<endl;
}
//get character
getch();
}

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

How does it work

No need to input anything. Directly prints the triangle.

Extending it

The program can be extended by extending the number of rows and columns. Go ahead and extend it…..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int (name them as you want.)
  5. Add the loop for number of rows.
  6. Add loop inside the loop added before for number of columns.
  7. Print the number.
  8. Increase number by one.
  9. The triangle is automatically generated.

At the end

You learnt creating the c++ program of Drawing a right angled triangle of numbers in c++. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Join India's Largest Code Learning Community on Uphub.io Get it on Google Play
Sharing options: