Right angle triangle star pattern in C++ using while loops

  c++, loops, while-loop

I want a pattern in which for n=4 in 1st row it has 4 stars and in 2nd row I has 1 space & 3 stars and in 3rd row, it has 2 spaces and 2 stars and so on.
The code, I tried to solve this.

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    cout << endl;

    int i = 1;
    while (i <= n)
    {
        //Printing Spaces

        int space = i - 1;
        while (space)
        {
            cout << " ";
            space++;
        }

        //Printing Stars

        int j = 1;
        while (j <= n)
        {
            cout << "*";
            j++;
        }
        cout << endl;
        i++;
    }

    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT