-2

The following photo should illustrate what my output should result.

Output which I want

This is the code I've tried but it gives out a full triangle instead.

#include<iostream>
using namespace std;
int main()
{int n;
cin>>n;
for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
     if(j<=n-i)
    { cout<<" ";}
    else
    {
        cout<<"*";
    }
    } cout<<endl;
}
}
Afieif
  • 18
  • 3
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Stef Nov 07 '20 at 14:34

1 Answers1

0

The only thing you should change in your code is the if condition.

#include<iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) { //row
        for (int j = 1; j <= n; j++) { //column
            if (n-j>=i) cout << " ";
            else cout << "*";
        }
        cout << endl;
    }
}
llocika
  • 16
  • 2