How to write pattern programs in C in a few easy steps

If you are visiting this page then you must be having some problems in printing patterns. I remember I was in class 8 when we were taught nested loops and the first programs which we had to do using them were printing patterns. I always liked patterns till that day when this girl asked me to teach her how to print patterns. Now this was no ordinary girl, she was my first crush and I had to make an impression. She could never understand the usual way which I tried in the beginning. Back home I came up with this simple way of doing all basic patterns. Next day I was much more confident. I told her we’ll start by printing this simple pattern:

*
**
***
****

#1: Understanding the loop concept:

The first question she asked what loop will we use? You see you can use both for loops and while loops but she always preferred for loops. So we’ll go with for loops. Things to notice in this pattern:

  1. No. of lines
  2. Is the no. of stars increasing or decreasing per line and by how much?

And that’s it. You can start coding. As you know you’ll need nested loops (for loops because she liked it). The first for loop will tell you about the no. of lines and the second will print the stars. One thing you must keep in mind that the limiting value of the inner for loop must depend on the outer for loop. In this case its j<=i. Okay so try to do it yourself and check it with the code.

//Pattern 1
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j;
printf("Enter the no of lines: ");
scanf("%d", &n);
for(i=0;i<n;i++){ //number of lines
for(j=0;j<=i;j++){ //number of stars in each line
printf("*");
}
printf("\n");
}
getch();
return 0;
}

#2: Two inner loops to print a pattern:

Now this is what she told me- I could write this program by myself. Though I was heart broken, I regained composure and told her to print this pattern:

    *
   **
  ***
 ****
*****

There’s an interesting thing about this pattern. It surely looks different because there are spaces before stars but other than that its same as the previous pattern. She didn’t believe me and also she didn’t have any idea of how to do it. Things to notice in this pattern: 1. No. of lines 2. No of triangles 3. Is the no. of stars increasing or decreasing per line and by how much? Now what are these triangles? You see there are 2 triangles in this pattern. One made my the spaces and another made by the stars.

pattern

So there will be 2 for loops inside the outer for loop. The first inner loop will print the spaces and the second one will print the stars. Also we must remember both the loops’ limiting value must depend on ‘i’. Now it may sound very confusing but lets see some pseudo code.

for i=0 to n{
for j=0 to n-i-1{    //limiting value of j is n-i-1
print(a space)
}
for k=0 to i{    //limiting value of k is i
print(a star)
}
}

Now try to do it yourself and check it with the code.

//Pattern 2
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j,k;
printf("Enter the no of lines: ");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
printf(" ");
}
for(k=0;k<i;k++){
printf("*");
}
printf("\n");
}
getch();
return 0;
}

Now this made her pretty happy. But there was one problem. The number of lines getting printed was one less than the number of lines asked to print. If you look carefully the number of lines getting printed are correct. Only 1 star per line is missing. For example in the first line, there is no star. In the second line there is 1 star. She managed to debug it herself. If you can’t then change the line 12 to

for(k=0;k<=i;k++){

#3: A more complex pattern program:

Finally there was this pattern which was our homework and she told me to help her out.

   1
  121
 12321
1234321
 12321
  121
   1

I won’t lie but all my confidence flushed through the drain because this was difficult but I couldn’t tell that to her. Instead I told her it’s very easy. And we need to approach it the same way we did the pattern number 2. She asked me to help her out with the triangles and i drew this. Make sure you try to draw it first and check it with my one.

pattern3

Yes there are 6 triangles but out of them only 3 are needed at a time. So we divide the pattern into two halves- top and bottom. And we do both of them separately. There will be 2 outer for loops and there will be 3 nested for loops inside each outer for loop. She told that she’ll write it herself and i told her to show me the pseudo code. With a little help she wrote and it was correct.

//1st outer for loop
for i=0 to n{
for j=o to n-i-1{
print(a space)
}
for k=0 to (equal to) i{
print(k+1)
}
for l=i-1 to (equal to) 0{ //decrement l
print(l+1)
}
}
//2nd outer for loop
for i=0 to n-2{
for j=0 to (equal to) i{
print(a space)
}
for k=0 to n-i-2{
print(k+1)
}
for l=n-i-2 to (equal to) 0{ //decrement l
print(l+1)
}
}

Now try to do it yourself and check it with the code.

#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j,k,l;
printf("Enter the no of lines: ");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
printf(" ");
}
for(k=0;k<=i;k++){
printf("%d",k+1);
}
for(l=i-1;l>=0;l--){
printf("%d",l+1);
}
printf("\n");
}
for(i=0;i<n-1;i++){
for(j=0;j<=i;j++){
printf(" ");
}
for(k=0;k<n-i-2;k++){
printf("%d",k+1);
}
for(l=n-i-2;l>=0;l--){
printf("%d",l+1);
}
printf("\n");
}
getch();
return 0;
}

#4: Some homework:

After the program compiled and ran properly she was so..uh excited that she gave me a hug. After I returned home I was thinking of her ( I actually thought she was the one). I was trying to find a way how to start a conversation with her the next day. Then I took out a piece of paper and wrote a few common patterns for her to practice:

question 1:
n=3
*****
 ***
  *
 ***
*****

question 2:
n=4
*******
*** ***
**   **
*     *
**   **
*** ***
*******

question 3:
n=4
1      1
33    33
555  555
77777777
555  555
33    33
1      1

I managed to give it to her but I don’t think she ever tried these programs. Anyway you can always give it a try. Now I know there are 1001 better ways to do these patterns but for beginners these programs might help. You will find the answers to the questions at http://www.intechgrity.com/how-to-write-pattern-programs-in-c-in-a-few-easy-steps-answers/. Hopefully you enjoyed reading this tutorial. If you have any doubts please use the comments.  Thank you and visit www.intechgrity.com for more tutorials.

194 comments

  1. Nanabielah

    Please help me to do cross pattern in c programming

  2. Silent

    kindly help me on this:
    123454321
    1234 4321
    123 321
    12 21
    1 1

    • Umair Riaz

      #include
      #include
      int main()
      {

      int i,j,n=7;

      for(i=0;i<=n;i++){
      for(j=0;j<=n;j++){

      if(j<=n-i)
      printf("%d",i);
      else
      printf(" ");
      } printf("\n");}
      }

  3. Sachim

    Please make this program
    1
    3 5
    7 9 11
    13 15 17 19
    21 23 25 27 29

  4. Vivek Singh

    * * * *
    * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * *
    * * * *
    how to print nested for loop in c programming

  5. sdf

    *********
    ****************
    ********************************
    ***************************************
    ************************************************
    ************************************************************
    ************************************************
    ***************************************
    ******************************
    ***************
    *********

  6. Bharath NS

    There seems to be a better solution for problem #2: O(n)

    #include
    void StairCase(int n) {
    for(int i=0;i<n;i++){
    printf("%.*s", n-i, " ");
    printf("%.*s#\n", i, "################");
    }
    }

    int main(int argc, const char * argv[]) {
    // insert code here…
    printf("Hello, World!\n");
    StairCase(6);
    return 0;
    }

  7. Samiullah Shaikh

    #include
    #include

    main()
    {
    char i,j;
    for(i=1;i<=26;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("%c",'A'+j-1);
    }
    printf("\n");
    }
    getch();
    }

    out put;
    A
    AB
    ABC
    ABCD
    ABCDE
    ABCDEF
    ABCDEFG
    ABCDEFGH
    ABCDEFGHI
    Upto "Z"

  8. saber

    2- Using spiral model develop the program to insert, delete and search linked list

  9. saranya

    how to print this pattern
    i/p->
    4
    o/p->
    ( ( # # ) )
    ( ( # # ) )
    ( ( # # ) )
    ( ( # # ) )

Comments are closed.