Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;
}

”’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading