Pattern Programs in C Made Easy – Part 2
Developing Pattern Programs in C Programming 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 Programming Methods in C with a deep analysis and a descriptive explanation on 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 How Does a For Loop Work. 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 set / sequence of commands (statements) repeatedly. It executes a set of statements till a particular condition evaluates to True.
Before going ahead with this Program, I would suggest you to check out Pattern Program – Part 2 to clear your Fundamentals about Pattern Programming.
Let us Look at a C Program to print the following Pattern:
1 2 3 4 5
2 3 4 5
3 4 5
for(i = 1; i <= 5; i++)
for(j = i + 1; j <= 5; j++)
Here, we have to print the Numbers (1 To 5) in multiple rows and every row must contain one digit less than the previous row.
The Outer For Loop is for Rows and the Inner For Loop is for Columns.
Initially, Variable i is initialized with 1 and prints the Value 1 on the Console Screen. Next, the program control comes into the Inner Loop. Now, the Variable j is initialized with a Value of i incremented by 1. This Inner Loop prints the values 1 to 5 in the First Row.
As soon as the Condition becomes False, program control comes out of the Loop and goes to the Outer Loop. Since, i is still less than 5, it again goes into the Inner Loop and this time j is initialized with 3 as j=i+1. This loop again executes till the Condition does not evaluate to False.
This sequence of Outer Loop will continue till the Value of i is not equal to 5. As soon as i=6, the Condition i<=5 will evaluate to False and the program will come out of the loop.
Note: We have used printf(“n”) after the inner loop as it takes the program cursor to the next line after printing the First row.
This Guide on Developing Pattern Programs in C Programming is very easy if you understand the Looping Concepts clearly. Initially, it may look intimidating. But, eventually it will get easier.