Pattern programs in c

In programming, patterns are some shapes and sequences that we draw to practice and understand the concepts of loops. There are too many ways to understand and master the concept of loops but patterns are considered not not only easy but also effective way of learning loops. In this post, we will be learning the concepts behind any type of pattern with code examples to boost your level of understanding effectively. I will also elaborate some pattern programs in c and c++.

Concept of pattern programs in c and c++

pattern programs in c
pattern programs in c
Starting from the simplest example of printing patterns, there are few things that one should  keep in mind. As you can see in the above figure, we need a nested loop with five iteration. The parent loop will be responsible for printing rows while the child loop will print stars horizontally. Firstly, the child loop will print 1 star when value of both k and j is 1. In the second iteration, it will print two stars and so on. As the value of k increase, new rows will be created to print stars with increment of 1 each time. So, we found the sequence in this pattern which can be implemented by the condition (j<=k). This means that j loop will print stars equal to the value of k loop. Lets see the code of the first pattern programs in c.

Code of pattern programs in c and c++


Code in c:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>// code in c programming language
int main()
{

  for (int j = 1; j <= 5; j++)  // to create rows
  {
    for (int k = 1; k <=j; k++) // to print stars in row
      printf("*");
    printf("\n");
  }

  return 0;
}

Code in c++:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <iostream>// code in c++ programming language
using namespace std;
int main()
{

  for (int j = 1; j <= 5; j++)  // to create rows
  {
    for (int k = 1; k <=j; k++) // to print stars in row
      cout<<"*";
    cout<<endl;
  }

  return 0;
}

output of the above program is as follows:
pattern programs in c
pattern programs in c

Now lets jump to another example which is little complex then previous example but if you have developed understanding of the previous one, then you will find it very easy to understand. The understanding of loops and patterns can only be developed by practicing it yourself. The notion diagram of second pattern programs in c and c++ is as follows.


By analyzing the above figure, we can find out that we need a nested loop with decrement operators . The parent loop is represented with green and the child loop with yellow color. The parent loop is responsible for printing new rows. In addition, yellow child loop will print number equal to the value k in  rows  (j>=k) times. Unlike previous example, both the loops will execute in reverse order using decrement operator.e.g(j--). Firstly, the yellow child loop will print 5 once when the value of both j and k is 5. In the second iteration, child loop will print 4(the value of k) twice because the loop condition(j>=k) will become true two timese.g(5>=4  && 4>=4). The third iteration will print 3 thrice when the value of k is 3 and so on. The thing that differentiate this example of pattern programs in c from previous one is the use of decrement operator.e.g(j-- /k--).

code of this example is

Code in c:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stdio.h>// code in c programming language
int main()
{

 for (int k = 5; k >= 1; k--)  // to create rows
 {
  for (int j = 5; j >= k; j--) // to print stars in row
   printf("%d",k);
 printf("\n");
 }
 return 0;
}


Code in c++:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>// code in c++ programming language
using namespace std;
int main()
{

 for (int k = 5; k >= 1; k--)  // to create rows
 {
  for (int j = 5; j >= k; j--) // to print stars in row
   cout << k;
  cout << endl;
 }
 system("pause");
 return 0;
}

output:


conclusion of the pattern programs in c and c++

In conclusion, I want to make one thing very clear that all  pattern programs has similarities with each other which I demonstrated in above stated examples. Both of these examples were using nested loops. But the first example of pattern programs in c and c++ were using incremental for loop while the second example  was created by decreasing the value of the iterator. Every pattern program will have at least one nested loop with parent loop responsible for printing rows and child for printing *,# etc. You can print too many patterns using this strategy.
I hope you found this post helpful. Please share this post with your friends and feel free to ask questions in the comment section. 



Post a Comment

0 Comments