-2

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


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

user4581301
  • 33,082
  • 7
  • 33
  • 54
Omi
  • 1
  • 2

2 Answers2

0

To print the right angle triangle we can use the string and then add some of the part to it to increase the length of string which looks similar to a triangle.

Here is my code:

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;
   }
} 
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • and what if we have to invert the right angle triangle then the concatenation won't work as you have to decrement In the loop? – Omi Nov 06 '21 at 05:20
-2

//Try this Code to print right angled triangle in c++

#include<bits/stdc++.h>

using namespace std;

void printPattern(int n)
{

    // Variable initialization

    int line_no = 1; // Line count
 

    // Loop to print desired pattern

    int curr_star = 0;

    for (int line_no = 1; line_no <= n; )

    {

        // If current star count is less than

        // current line number

        if (curr_star < line_no)

        {

           cout << "* ";

           curr_star++;

           continue;

        }
 

        // Else time to print a new line

        if (curr_star == line_no)

        {

           cout << "\n";

           line_no++;

           curr_star = 0;

        }

    }
}
 
// Driver code

int main()
{

    printPattern(7);

    return 0;
}

//Output of the code

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Nasyx Nadeem
  • 241
  • 2
  • 12
  • 1
    Don't `#include`. Some kid could find it. [Details](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Nov 06 '21 at 05:18