C++: Printing an Asterisk Triangle
C++, CBSE, Code, Computer Science, Programming
Yet another beginner's program and a popular one. Simulating "graphics" with characters is one of the best parts in C++ and believe me -- not all of them are easy to get right. Aligning, implementing the right code and deciding the structure is critical as the program's appearance is a vital factor.
Now, that's the output we require. The * (Asterisk) is optional and you can use any other character like & or # but for me, * seems pretty. At a glance, we can observe that the nth row has n asterisks. Moreover, indentation of these asterisk rows decreases as we go down. Hence, we will be needing some loops to control the row placing, the spacing as well as for printing the asterisks. So, here's the final code to obtain the output.
Source Code
#include<iostream> #include<stdlib.h> #include"conio.h" using namespace std; void main() { system("cls"); int n,i,j; cout<<"\nEnter the number of stars: "; cin>>n; for(i=0;i<n;i++) { cout<<"\n"; for(j=0;j<n-i;j++) cout<<" "; for(j=0;j<=i;j++) cout<<"* "; } getch(); }
Analysis
We have used our usual header files here for I/O services. As you might have guessed it, this is a rather simple program. If not, well, you may need to practice harder. As the program demands, we have asked for the user to enter a value, n, which will be used as the limiting value. Here, n is the max number of rows our triangle is going to have.
Now, observe the first loop. I've started the control variable, i from 0 and that means the condition must be given as i<n. You can start with 1 instead but remember that the condition must be changed to i<=n as n is inclusive in the condition.
In the first loop, we are adding individual rows. The first statement within the code block is to insert a newline. Now, since we need to get a uniform triangle with proper indentation, we first start another loop inside the main loop to print spaces. Since the spaces are maximum at the beginning and minimum at the end, we can give a condition with a new control variable as j<n-i, i.e, the spacing decreases with increasing i. This single line loop will add the proper spacing for the nth row.
Finally, we give yet another loop to print out asterisks which are the most important part of the program and do remember that after every *, I have given a space also as this makes it a standard equilateral triangle rather than a right-angled equilateral triangle. If you remove the space from this cout statement, you will get a right triangle instead.
That's it. Its that easy. Now, here's another puzzle for you. Now that you know how to get the ouput of a fully filled Asterisk Triangle, can you print a bordered Asterisk Triangle instead? See the image below to get a visual. ATB. :)
Now, observe the first loop. I've started the control variable, i from 0 and that means the condition must be given as i<n. You can start with 1 instead but remember that the condition must be changed to i<=n as n is inclusive in the condition.
In the first loop, we are adding individual rows. The first statement within the code block is to insert a newline. Now, since we need to get a uniform triangle with proper indentation, we first start another loop inside the main loop to print spaces. Since the spaces are maximum at the beginning and minimum at the end, we can give a condition with a new control variable as j<n-i, i.e, the spacing decreases with increasing i. This single line loop will add the proper spacing for the nth row.
Finally, we give yet another loop to print out asterisks which are the most important part of the program and do remember that after every *, I have given a space also as this makes it a standard equilateral triangle rather than a right-angled equilateral triangle. If you remove the space from this cout statement, you will get a right triangle instead.
That's it. Its that easy. Now, here's another puzzle for you. Now that you know how to get the ouput of a fully filled Asterisk Triangle, can you print a bordered Asterisk Triangle instead? See the image below to get a visual. ATB. :)