Upright Hollow Triangle with given base size – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#3111 siteicon   siteicon   siteicon  

Upright Hollow Triangle with given base size

Printing upright hollow triangle with given base size.

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++ #3111


Source Code

  Related (?) :

Run Output

* *
* *
* *
* *

Code Understanding

int n=5; //This is given base size unit n when actual base would be 2*n+1;
int i,j,k=0; //Initialised for loop counting
int sp;  //Used for space counting
int g=n;  //Used for gap initialisation
for (i=1; i<n+3; i++)  //outer loop for row counting.
for (sp=g; sp>=0; sp–) cout << ” “;  //For space before the first * in a row
for (j=1; j<k; j++)                //This loop prints the row elements
if(j==1 || j==(k-1) || i==(n+2)) cout << “*”;
character * is printed in specific conditions as given
else cout << ” “; //These are spaces inside the triangle.
cout << endl;         //This helps to move to next row.
k+=2;                     //This adjusting k for next row of *
g–;                         //This adjusting initial gap for printing the next row

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): upholtri.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