Inverted triangle with user given character
Printing an inverted triangle filled with user given character.
Learning Objectives
- Print a pattern resembling a downward triangle filled with characters given by the user.
Source Code
|
|
Related ( ? ) : |
Run Output
Code Understanding
int i,j,k,r; char ch; //Relevant variables declared
cout<<“Give row count :”; cin>>r; //Row count collected from user.
cout<<“Give character :”; cin>>ch;//The character to be filled is collected
for(i=1;i<=r;i++) { outer loop for row counting
for(j=0;j<i-1;j++) cout<<‘ ‘;
Loop for printing spaces before character, This keeps increasing in each iteration as this is an inverted triangle
for(k=(r-i+1)*2;k>1;k–) cout<<ch;
Inner loop for printing the character given. It starts from given variable*2 as double no. of characters around the mid point is to be printed.
cout<<endl; //Given after inner loop to go to next row.
}
Suggested Filename(s): dn-tri-pat.cpp,dtripat.cpp
Created: 19-Jan-2018 19:20 | Updated: 19-Jan-2018 23:26|
