Pattern Programs in C – Part 1

By | September 5, 2015

Pattern Programs in C Made Easy – Part 1

Developing Pattern Programs in C Programming Language along with the For Loop is one of the most daunting and intimidating topics in C Programming.

It is one of the most frequent topics the Interviewers generally grill the candidates on. This article will definitely help you out on How to Build Pattern Programs in C Programming Language and get a good hold over such kind of programs.

We have tried to get you the best Pattern Programs in C with a deeper analysis and a descriptive explanation of Pattern Programming. We will focus on developing the Algorithm and Logic Building Approach with the following program.

Let us First start with some basics and eventually we shall move on some complicated topics.

Before we indulge ourselves into Pattern Programs, it is very important to know about a how a For Loop works. Every Pattern Program needs a Looping Control System. Usually, we use For Loop because of its easy to use approach. A For Loop executes a series / sequence of commands(statements) repeatedly. It executes a set of statements till a particular condition evaluates to True.

How Does a For Loop Works

Steps:

  • Initialization
  • Condition Checking
  • Execution of Statements within the For Loop
  • Increment/ Decrement

Above mentioned steps execute sequentially. These steps keep on repeating until the Condition evaluates to False.

Let us learn more about a For Loop by an example.

Print a Series from 1 to 5 in a Row:

1      2      3      4      5

Source Code

Output

C Program To Print Pattern

Description

Here, we need to print the numbers 1 to 5 in a row(in a single line). For this, we have used a For Loop.

 

First Iteration:

Here, we have initialized the Variable i with 1. The Loop Control then Checks for the Condition. Since, 1 is Less than 5, the Loop Control will go into the Statements Section wherein it will print the value of i onto the Console Screen.

Second Iteration:

After printing 1, the control moves to the Increment section in the For loop wherein it increments the value of i by 1. i++ is equivalent to i=i+1. Now, i=2. The condition will be evaluated. Since condition evaluates to be True, it again goes into the Statement section and prints the Current value of i.

 

Third Iteration:

After printing 2, the control moves to the Increment section in the For loop wherein it increments the value of i by 1. Now, i=3. The condition evaluates to be True. Therefore, it again goes into the Statement section and prints the Current value of i.

Fourth Iteration:

After printing 3, the control moves to the Increment section in the For loop wherein it increments the value of i by 1. i++ is equivalent to i=i+1. Now, i=4. The condition will be evaluated. Since condition evaluates to be True, it goes into the Statement section and prints the Current value of i.

Fifth Iteration:

After printing 4, the control moves to the Increment section in the For loop and hence it increments the value of i by 1. Now, i=5. The condition will be evaluated. Since condition evaluates to be True, it again goes into the Statement section and prints the Current value of i.

Sixth Iteration:

After the Console prints 5, the value of i gets incremented by 1. It then checks the Condition and since this time it is False, the program control will come out of a loop. Hence, sixth time the Statement would not be executed.

This is how the Values from 1 to 5 get printed one by one in a single line or row.

Developing Pattern Programs in C Programming Language is very easy if you understand the Looping Concepts clearly. Initially, it may look intimidating. But, eventually it will get easier.

Let's Discuss