how to print a right angle triangle in c++ using single loop

this is the code that I wrote for printing the pattern using single however it doesn’t work can you help me out.

”’C++

#include<iostream>
using namespace std;
int main()
{
    int line, star = 0, n;
    cin >> n;
    for (line = 1; line <= n; line++)
    {
        if (star < n)
        {
            cout << "*";
            star++;
            continue;
        }
        if (star == line)
        {
            cout << endl;
            star 
        }
    }
    
    system("pause");
    return 0;
}

”’

>Solution :

To print the right angle traingle we can use the string and then add some of the part to it to increase the length of string which look similer to triangle.

void solve()
{  
   string a = "*";
   string add_to_a= "*";
   int n;
   cin>>n;
   for (int i = 0; i < n; ++i)
   {
       cout<<a<<"\n";
       a+=add_to_a;
   }
} 

this is code written by me this may help

Leave a Reply