Inverted triangle with user given character – CSKC
Sign In

Search

Code Learning #CPP#2804    siteicon   siteicon  

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

TC++ #2804

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

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



Share

Created: 19-Jan-2018 19:20 | Updated: 19-Jan-2018 23:26|