Answer to Question #329171 in C++ for Zaki

Question #329171

Write a C++ program that asks the user to enter a number N .After that, print N lines using asterisk (*) in a


triangle shape as shown below;


By using nested loop

1
Expert's answer
2022-04-15T16:34:30-0400
#include <iostream>
using namespace std;

int main() {
    int n;

    cout << "Enter a number: ";
    cin >> n;

    for (int i=0; i<n-1; i++) {
        for (int j=0; j<n-i-1; j++) {
            cout << ' ';
        }
        cout << '*';
        if (i) {
            for (int j=0; j<2*i-1; j++) {
                cout << ' ';
            }
            cout << '*';
        }
        cout << endl;
    }
    for (int j=0; j<2*n-1; j++) {
        cout << '*';
    }
    cout << endl;

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS