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
#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;
}
Comments
Leave a comment