Downward Hollow Triangle with given base size and left offset – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#3116 siteicon   siteicon   siteicon  

Downward Hollow Triangle with given base size and left offset

Printing downward hollow triangle with given base size and left offset.

Learning Objectives

  • Learn the logic of leaving spaces before printing a character and then after the character for internal spaces while printing patterns.


Source Code

TC++ #3116


Source Code

  Related (?) :

Run Output

* *
* *
* *
* *

Code Understanding

int n=5;  //Number of base units where actual base size would be 2*n+1
int i,j,k=n*2+1;
i,j,k are loop indexes. k=n*2+1 as the first row will be the base in downward triangle
int sp; //For space counting before the printing of first * in a given row
int g=2; //Initial gap or left offset of the whole downward triangle.
for (i=1; i<n+3; i++) //Outer loop to print rows
for (sp=1; sp<=g; sp++) cout << ” “; //Loop to print initial spaces before first character
for (j=k; j>=1; j–)  //Internal loop to print each row element
if(j==k || j==1 || i==1) cout << ‘*’;
This condition is to print * only when given condition is met.
else cout << ‘ ‘;
This space would be internal spaces within the triangle
cout << endl; //new line after each row is printed
k-=2; //Preparing internal loop count for next iteration
g++;  //Preparing initial gap for next iteration


  • One can produce a filled triangle by filling a different character in the internal loop.

Common Errors

  • This code is definitely highly error prone. Students are advised to experiment with different loop settings to get every thing correctly.

Suggested Filename(s): dnholtri.cpp


sunmitra| Created: 28-Jan-2018 | Updated: 28-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues