Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello; Iam trying to write a code to display this output exactly in the middle o

ID: 3623944 • Letter: H

Question


Hello;

Iam trying to write a code to display this output exactly in the middle of the screen:

*
**
****
********
my problem is :1: I dunno how to display it like that in the middle!!

2- I want the stars in each row to be the double the row before!!

Here is my code:

#include <iostream>
using namespace std;

int main()
{
int n, i, j;

cout << " Please enter the length of a triangle: " ;
cin>> n;

for( i = 1; i <= n; i++ )
{

for( j = 1; j <= i; j++ )



cout<<"* ";
cout<<" ";


cout<<endl;
}

return 0;
}

Here is my output:"

Please enter the length of a triangle: 4
*
* *
* * *
* * * *
Press any key to continue . . ."

Thank you !!

Explanation / Answer

I changed you inner "for" loop and used the formula j=j*2; , and also initialized int j=1


#include <iostream>
using namespace std;


int main(){

int n, i,j=1;

cout << " Please enter the length of a triangle: " ;
cin>> n;

for( i = 1; i <= n; i++ )
{

for( int n=0; n <j; n++ )
cout<<"* ";

j=j*2;

cout<<endl;

}

return 0;
}