Powered By Blogger

Wednesday 11 March 2020

Let's Learn some Pyramidal structures using 'C' programming (i.e. printing number pyramid with '*')

Hello viewers, your heartiest welcome to my blog.
Today in this blog I am going to discuss a number pyramid structure using 'c' language.
I think you all programmers or beginners have already known the simple '*' triangle printing, don't worry if it is not well understood by you. I will try my best to make you habituated with all these things.
So, let's start.
Before going into the complex structure, we first try to understand the simple triangle in short.
In these cases of star printing we need to use for loop or while loop etc. because we need to do the same work for many times (i.e. printing '*','**','****' etc), so it is better to use loop functions. One may think about printing stars in increasing or decreasing order as given in the question by using printf function with a new line character(\n) without any looping. But when you will face a situation of printing more than 10 or 20 or 30 or >30 lines pyramid, it will not be easy to print the pyramid with only 'printf ' function and moreover, it will be a time-consuming process also. So, while printing 'star(*)' pyramid we should use some loop functions, most of the cases it is "for loop". Hope you understand why we use loop function in various structure printing in 'C'.
Let's see in the following:
You are asked to print a star triangle with one star incrementing and having five lines in the pyramid.
You might have done it earlier, for this structure we first check how many lines (rows) are there in the pyramid. It has 5 lines so, we should set a loop that will iterate(execute) 5 times,so we set a loop which will start from 1 and execute total of 5 times. That's not all, we also need to print stars according to increasing order. So, we must think with the iteration of the for loop, there should be another loop that print stars, add a new line and increment stars at their respective positions.
We must be careful about that the second loop should be inside the first 'for loop' and this type of looping is known as "Nested For Loop". Otherwise, star printing will not be in correct order.
Let's see the programming:

We have taken first for loop that will iterate 5 times, but inside it there's another loop. let's look inside the second loop. Here in the condition checking j<=i that mean when the value of i=1,j<=1, so it will print only one '*', then it goes back to first loop again. At this time the value of i=2 and then it enters in the inner loop and it checks condition j<=2(as the present value of i=2), so the inner loop will iterate two times printing 2 stars('**'). Then control goes to first loop again and i becomes i=3,then inner loop j<=3(as present value of i=3),so loop iterate 3 times printing 3stars('***'). And this process goes on until i=5.
It should be noted that every time after printing stars control goes to the next statement that is "new line character"(\n) and prints a new line each time after the completion of the inner loop. This way the whole process goes on.
There's a short program summary


#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)           /*Outer for loop iterate 5 times*/
{
for(j=1;j<=i;j++)  /*inner 'for loop' iterate 'i' times for every iteration of outer 'for loop'*/
{
printf("*");  /*printing '*' */
}                             /* end point of inner for loop'*/
printf("\n");        /*printing new line character*/
}                                    /*end point of outer for loop*/
}

Thank you.

No comments:

Post a Comment

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...