Triangle in C++
Q. Write a C++ program number triangle ?
#include <iostream>
using namespace std;
int main()
{
int n, i, j, c = 1;
cout << "Enter the no of rows for Floyd's triangle: ";
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
cout << " " << c;
c++;
}
cout << endl;
}
return 0;
}
Q. Write an algorithm to number triangle in c++?
1. Start
2. Initialize variables:
- Read integer `n` from user input (number of rows).
- Initialize an integer `c` to 1 (this will hold the current number to print).
3. Outer loop: Run loop variable `i` from 1 to `n` (inclusive), where `i` represents the current row.
4. Inner loop: For each row `i`, run a loop variable `j` from 1 to `i` (inclusive), representing the number of elements in the current row.
- Print the current value of `c` followed by a space.
- Increment `c` by 1.
5. After the inner loop finishes (all elements for the current row are printed), print a newline to move to the next row.
6. Repeat steps 4-5 for all rows.
7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com