Question

Program in c++ to print a triangle of numbers?

How to write a program to print

1

12

123

1234

Answers

You will need 2 loops for this program.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr(); //To clear the screen

for(int i=1; i<=4; ++i)

{

for(int j=1; j<=4; ++j)

cout<<j;

cout<<"\n" //To print each line of numbers in separate lines

}

getch();

}

You will get the output as:

1

12

123

1234

If you want the user to input the number of lines, write cin>>n; before the for loops(just after the clrscr(); statement) and then put i<=n instead of i<=4 in the first for loop.

You can also do this with other loops(for, while, do-while)

Enjoy. :)

#1

simple use for statement give the condition as your integer<=4 and print your integer with \n you will get the answer

#2

For this you will need to use 2 loops .. one from left to right and one from top to bottom. Something like this;

while (cin >> 4 >> 4) {

for (int row=0; row<height; row++) {

for (int col=0; col<width; col++) {

cout << col;

}

cout << endl; // end the line of stars.

}

}

#3

Can you Help me how to write the code to print the numbers 1 to 10 in the following manner:12345678910With Explanation plz.. im unable to understand how the looping carries out..

#4