How to Write a C program to print Pyramid of Numbers


C is a widely used programming language and also it is considered as a one the hardest programming language which is in the use. 

Though this guide is mostly driven with C programming language, it will not be a big issue to perform the same task with a different programming language, if you understand the logical thinking behind printing number pyramids.

Star patterns and these number patterns are a great way to practice your basic programming knowledge. Without much more talk let's get into the first pattern of our guide. 

The pyramid of stars is much easier than number pyramid and that's where we are going to start.
Here we have the pattern. Before jumping into the text editor, let's simplify this bit more. First, we have to understand this pattern in a way which computer can understand. Then the coding it just writing that relationship in another way.


For better understanding let's restrict the pattern for size 5. Then just overwrite 5 with a variable. So what we have to do here is we have to go through every element in every line and print a star whenever the absolute value of element is less than the line number, 

As an example when we are on the line 1 we just have to print star on element 0. And when we are on line 2 we have to print stars on element position -1 0 1 all which have the absolute value less than 2. 

So the first thing we need is a loop which goes from 1 to 5 which will navigate us through all the lines.


int i;
int main()
{
for (i = 1; i <= 5; i++){
 
    printf("\n");
}
}
So what does this segment of code does is navigate us through all five lines which we need? Then we can add another loop inside this loop to navigate the elements which go from -4 to the line number.


int i;
int j;
int main()
{
for (i = 1; i <= 5; i++){
    for (j = -4; j < i; j++){
     
    }
    printf("\n");
}
}
Then we need a function to calculate the absolute value of the element. which can be easily defined.


int absoluteValue(int a){
    if (a < 0){
        return -a;
    }else {
        return a;
    }
}
Now we have, all we need, the only thing remaining is printing a star when the absolute value of the element is less than line number and printing a space. So the final code which gives the desired output can be expressed as follows with this final modification.


int absoluteValue(int a){
    if (a < 0){
        return -a;
    }else {
        return a;
    }
}
int i;
int j;
int main()
{
for (i = 1; i <= 5; i++){
    for (j = -4; j < i; j++){
        if ( (absoluteValue(j) - i) < 0 ){
            printf("*");
        }else{
            printf(" ");
        }
    }
    printf("\n");
}
}
Congratulations! you have successfully written the code for a pyramid of stars. So without much more cheering let's move on the second pattern.

So take a close look on this, and try to understand the difference between this and the previously printed pattern. Here instead of printing a star, we have to print the number of the line where we are currently standing on. 

So we can modify the above code by only changing one letter. Replacing '*' with 'i', which means printing the current outer loop number.


int absoluteValue(int a){
    if (a < 0){
        return -a;
    }else {
        return a;
    }
}
int i;
int j;
int main()
{
for (i = 1; i <= 5; i++){
    for (j = -4; j < i; j++){
        if ( (absoluteValue(j) - i) < 0 ){
            printf("%d", i);
        }else{
            printf(" ");
        }
    }
    printf("\n");
}
}
Now we have done a lot, learned a lot, however, the hardest of all is still there. Let's code the number pyramid.

Before going much deeper, I highly encourage you to stop reading this now for a while and to figure out a way to code this yourself. 

We can clearly observe that every line starts and ends with its exact line number and the printed value increment while element number 0 reached and then it starts to decrement again.

So what we have to do is pretty simple. Keep another variable called "adder" and assign it the value 0 within every cycle of the outer loop. And within the inner loop, every time a number is gonna printed, We need to increment or decrement the value of  "adder". If the value of inner loop or the element number is less than 0 we need to increment the adder else decrement it.

With this modification, we can obtain the working desired code for the number pyramid as follows.


int absoluteValue(int a){
    if (a < 0){
        return -a;
    }else {
        return a;
    }
}
int i;
int j;
int adder;
int main()
{
for (i = 1; i <= 5; i++){
    adder = 0;
    for (j = -4; j < i; j++){
        if ( (absoluteValue(j) - i) < 0 ){
            printf("%d", (i + adder));
            if (j < 0){
                adder++;
            }else{
                adder--;
            }
        }else{
            printf(" ");
        }
    }
    printf("\n");
}
}

Congratulations, you have just successfully written a C program to print the pyramid of numbers. I apologize for making this little bit longer, however, I just needed to describe every little step and also to describe the logical thinking behind those steps. Hope this was helpful to you.

If you have any question to ask the comment section is all yours. and also let me know what you think through a comment, that will be a great help for me to improve things. And don't forget to share with your friends they may be also loking for a way to figure this out. 


How to Write a C program to print Pyramid of Numbers How to Write a C program to print Pyramid of Numbers Reviewed by Thiranja Lakrandika on January 01, 2018 Rating: 5

No comments:

Linked List implementation in C

Even-though arrays are a super cool super fast way to store and use data, You need to know the exact amount of data you need to store at...

Powered by Blogger.