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. poulami

    Wil u plz help me in printing the following pattern??
    3
    44
    555
    6666
    6666
    555
    44
    3

  2. archana

    Well, I did understood the main logic behind the pattern.
    But I have only one problem.
    How do we initialise the value of for loop? I mean how do we come to know the for loop shud b initialised at i=0 or i=2*(j-1) . Please help me understand it.

    • Raman dwivedi

      initialize means the starting value …see from where you have to start like if u have to make a program of 2 multiplication table then

      for(i=1;i<=10;i++)
      {
      printf("%d",2*i);
      }

  3. nurul izzaty

    can you show the programming for shape half diamond?

  4. Suganthi.H

    i have print the number in 5.The result is 1 2 3 4 5 and output 5 4 3 2 1 in vertical series…how to write source code in c programming using for loop….pls reply me

  5. guna

    i understand.but it is difficult to wherewe have to put n-i-2,n-i,n-j…
    pleasse reply me soon

  6. mukul

    Finally i understand the logic behind print pattern program
    Thankyou!!

  7. ankitdetroja

    loop is for how much time you are loop inside.. doesn’t depend on init value..
    for(int i=0;i<=5;i++) mean.. i m going to move 0,1,2,3,4,5.. times..
    value doesn't matter.. you can define value inside loop

  8. maniraj

    send the code for this pattern.
    1**********1
    12********21
    123******321
    1234****4321
    12345**54321
    123456654321

  9. kiran kamble

    sir i’m confuse, how i know rectangle program with binary code? sir tell me answer plz

  10. MD ASIF NAWAB

    Dear sir i have some confusion in this cide that is
    L=i-1 and l<=0.what does it mean.
    If l=0 and i=0 thenthat means l=0-1=-1 again 0<=0 is true but after increase +1 it will be now 1<=0 false but how it print a line when first is true and other line is false.

    • arnab Post author

      If you check the code below you will see I have written “l>=0”. I hope that answers your question.

  11. PRATEEK

    help me to write this pattern..????

    *
    *#*
    *#*#*
    *#*#*#*#
    *#*#*#*#*#

  12. Afzal Gogda

    I want below number pattern in c program.

    1 2 3 4
    8 7 6 5
    9 10 11 12
    16 15 14 13

    • Farhan

      #include
      #include
      int main()
      {
      int i,j,num[4][4]={ {1,2,3,4},
      {5,6,7,8},
      {9,10,11,12},
      {13,14,15,16}};
      clrscr();

      printf(“\n\n\t”);
      for(i=0 ; i3) break; }
      else
      { j–; if (j<0) break; }
      }
      printf("\n\t");
      }
      getch();
      return 0;
      }

      or

      #include
      #include
      int main()
      {
      int i,j,n,val;
      clrscr();
      printf(“\n Enter a value 19)
      { val=19; printf(” Sorry only upto 19 are allowed:\a”);
      }

      printf(“\n “);
      for(i=n=1 ; i<=val ; i++)
      {
      for(j=1;j<=val;j++)
      {
      printf("%d ",n);
      if(i%2==0)
      n–;
      else
      n++;
      }

      if(i%2!=0)
      n–;
      else n++;

      n+=val;
      printf("\n ");
      }
      getch();
      return 0;
      }

  13. Amrut

    can i know how to print this ???
    1 2 3
    8 9 4
    7 6 5
    like numbers are in circular fashion.

  14. zarna

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

    give me a code for this pattern

    • Pralesh Rayamajhi

      #include
      #include
      int main()
      {
      int i,j;
      for(i=1; i<=6; i++)
      {
      for(j=1; j<=i; j++)
      {
      printf("*");
      }
      printf("\n");
      }
      getch();
      }

      • Deepika

        Can we print the same pattern with only one printf statement?

  15. pravin Nehatrao

    1 want below pattern in c

    1 2 3
    d e f
    4 5 6
    g h i

    • Farhan

      #include
      #include
      int main()
      {
      int i,j,n=1,n1=100,val; // n1=100 as ascii value of ‘a’=100
      clrscr();

      printf(“\n “);
      for(i=1 ; i<=4 ; i++)
      {
      if(i%2!=0)
      {
      for(j=1 ;j<=3 ; j++,n++)
      printf("%d ",n);
      }
      else
      {
      for(j=1;j<=3;j++,n1++)
      printf("%c ",n1);
      }
      printf("\n ");
      }
      getch();
      return 0;
      }

  16. Vidya

    I want to print any word on o/p screen which blinks from top to bottom

  17. Vidya

    In c I want to write a program to display ur name on output screen and
    blinks from top to bottom using for loop

  18. Priya

    I am facing problem with Program #3 line 27
    not getting the result but by changing it to(l-1).

  19. mano

    how to write on c program stars in m symbal
    tell me source code

    • ranju

      how to write on c program stars in m symbal
      tell me source code

  20. Sonu Yaduvanshi

    int main()
    {
    int i,j,k=1;
    int m[7][7]={0};
    for(i=1;i<=7;i++)
    {
    for(j=1;j<=7;j++)
    if(j==i || 8-i==j)
    m[i-1][j-1]=k;
    if(i<4) k++;
    else –k;

    }
    for(i=0;i<7;i++)
    {
    for(j=0;j<7;j++)
    {
    if(m[i][j]==0)
    printf(" ");
    else
    printf("%d",m[i][j]);
    }
    printf("\n");
    }
    return 0;
    }

  21. pradip pk

    thnx for this lesson.you are doing really a great work.

  22. yssa

    do you have codes for patterns forming a letter with a given character?

  23. Nikhil

    These all pattern ques have n*n complexity. Can we do this with n complexity

    • muthyala nagaraju

      #include
      #include
      void main()
      {
      int r,c;
      for(r=1;r<=5;r++)
      {
      for(c=1;c<=r;c++)
      {
      if(r%2!=0)
      printf("%d",c);
      else
      printf("*");
      }
      printf("\n");
      }
      getch();
      }

  24. anand

    i need logic for this kind patter pl help me.
    *
    *
    * *
    * *
    * * *
    * *
    * *
    *
    *

Comments are closed.