c program to print patterns

As you know, patterns are vital for learning and practicing the concept of loops. I have elaborated loops with some examples in my previous post in which I covered simple examples. This post will cover some complex examples to elevate your understanding of loops. I will elaborate c program to print patterns with code.

In this figure, it is crystal clear that this pattern has two parts  which is demonstrated with brown and green Rectangles. Both these parts can be printed through nested loops with three child loops which are demonstrated with different colors. Each part is a combination of different simple patterns which we have discussed in our earlier post. In first part demonstrated with brown border rectangle, we have two triangle patterns with decreasing number of cells and  a pattern of spaces in between them. Code of both increasing triangles will be same but separated with a pattern of spaces. In this c program to print patterns, lets deal the first part first.


The first part is comprised of a nested loop with three child loops where first and third loop is identical. The above figure is showing the first loop. As it is clear from the figure that parent loop will run 5 times while the child loop will print m until the condition (j<=6-m) is true each time.firstly, the value of m is 1 so the child loop will execute 5 times according to the condition (j<=6-m). In second iteration, the value of m is 2 so child loop will print value of m 4 times as shown in the above figure.  The parent loop will iterate as far as the parent loop condition m<=5 is satisfied and the child loop will print value of m until the condition j<=6-m is true.



The above dry run diagram is clearly showing the logic of spaces pattern. When the value of parent loop iterator m is 1, the condition of child loop k<m is not satisfied. So, the child loop will not execute. The value of m is 2 in the second iteration. Child loop condition(k<m)will come true when k=2 and  execute once printing two spaces. In the next iteration when m=3, the condition(k<m) will be satisfied twice and child loop will run two times printing four space. The next two iteration will execute on the same sequence. Keep in mind that I am printing spaces twice per child loop execution.
the code for the first part(brown triangle) is as follows.

Code of c program to print patterns

code in c


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>// C language 
int main()
{
    for(int m=1;m<=5;m++) //for creating rows
    {
        for(int j=1;j<=6-m;j++)
        {
            printf("%d",m);   //for printing first triangle
        }
        for(int k=1;k<m;k++)
        {
            printf("  ");    //for printing spaces
        }
        for(int j=1;j<=6-m;j++)
        {
            printf("%d",m);     //for printing second triangle.same code as first 
        }
        printf("\n");
    }
   
    return 0;
}

code in c++


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
    
    for(int m=1;m<=5;m++)
    {
        for(int j=1;j<=6-m;j++)
        {
            cout << m ;
        }
        for(int k=1;k<m;k++)
        {
            cout << "  ";
        }
        for(int j=1;j<=6-m;j++)
        {
             cout << m ;

        }
        cout << endl ;

    }
   
system("pause");
    return 0;
}

output up to now

Now lets start understanding the second part of this pattern(green rectangle). This part can also be printed using a nested loop comprising three child loops. The first and the third child loop, responsible for printing increasing cell triangles, are exactly same with a pattern of spaces in between to separate them. We will discuss first triangle and pattern of spaces only.

By looking at the figure, it is prominent that parent loop will execute 4 times. As child loop iterator j has initial value of zero, the child loop will execute twice in the first iteration.The child loop will print value of m thrice when value of i=2 according to the condition(j<=i) in the second iteration. In third iteration, the child loop will print 4 times and so on. It is very similar to patterns of stars  I explained in my previous post.


In this last diagram, it is clearly portrayed that the parent loop "i" will iterate 4 time as it's parent of both k and j. Looking at the child loop condition it is clear that it will repeat three times printing six spaces when i=1. Similarly, it will execute twice in the second and thrice in the third iteration. The parent loop ''i"  will repeat for the fourth iteration but the child loop "k" condition would become false. So, nothing will be printed in the forth iteration as shown in the above figure. Code of the green rectangle is given below.

Code for c program to print pattern

code in c


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main()
{
  
    for(int i=1;i<5;i++)
    {
        for(int j=0;j<=i;j++)
        {
            printf("%d",i);
        }
        for(int k=1;k<=4-i;k++)
        {
            printf("  ");
        }
        for(int j=0;j<=i;j++)
        {
            printf("%d",i);
        }
        printf("\n");
    }

    return 0;
}

Code in c++


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{

    for(int i=1;i<5;i++)
    {
        for(int j=0;j<=i;j++)
        {
            cout<< i;
        }
        for(int k=1;k<=4-i;k++)
        {
            cout<< "  ";
        }
        for(int j=0;j<=i;j++)
        {
            cout << i;
        }
        cout<<endl;
    }
     system("pause");
    return 0;
}

output up to now

Complete code in c


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>// C language 
int main()
{
    for(int m=1;m<=5;m++) //for creating rows
    {
        for(int j=1;j<=6-m;j++)
        {
            printf("%d",m);   //for printing first triangle
        }
        for(int k=1;k<m;k++)
        {
            printf("  ");    //for printing spaces
        }
        for(int j=1;j<=6-m;j++)
        {
            printf("%d",m);     //for printing second triangle.same code as first 
        }
        printf("\n");
    }
    for(int i=1;i<5;i++)      //parent loop
    {
        for(int j=0;j<=i;j++)    //for printing increasing triangle
        {
            printf("%d",i);
        }
        for(int k=1;k<=4-i;k++)    //for printing spaces
        {
            printf("  ");
        }
        for(int j=0;j<=i;j++)    //for printing increasing triangle

        {
            printf("%d",i);
        }
        printf("\n");
    }

   
    return 0;
}

Complete code in c++


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>// C language 
int main()
{
    for(int m=1;m<=5;m++) //for creating rows
    {
        for(int j=1;j<=6-m;j++)
        {
            cout << m;   //for printing first triangle
        }
        for(int k=1;k<m;k++)
        {
            cout << "  ";    //for printing two spaces per row
        }
        for(int j=1;j<=6-m;j++)
        {
            cout<< m;     //for printing second triangle.same code as first 
        }
        cout<<endl;
    }
    for(int i=1;i<5;i++)      //parent loop
    {
        for(int j=0;j<=i;j++)    //for printing increasing triangle
        {
            cout << i;
        }
        for(int k=1;k<=4-i;k++)    //for printing spaces
        {
            cout << "  "; 
        }
        for(int j=0;j<=i;j++)    //for printing increasing triangle

        {
            cout << i;
        }
        cout << endl;
    }

   
    return 0;
}

Final output


Conclusion

In conclusion,I want to tell you that this complex c program to print patterns is printed through the knowledge of looping. I combined different easier patterns and finally printed a complex pattern. Try to print this pattern yourself to practice your looping .Hope you found this post helpful. Kindly share it with your friends. If you have any suggestions or questions, don't hesitate to leave it in the comment box.     


Post a Comment

0 Comments