After learning the basics of the C Programming, let's play with the language and code some patterns. To able to code the patterns, you need to have proper knowledge ie. working of the loops, basically nesting of loops. Patterns are generated with the help of the loops. Let's revise the nesting of loops. So to create the pattern we need rows and columns. For rows and columns, we need two loops ie. one for changing the rows and other for changing the column. Suppose, you need to create in 5x5 then you need to set the loop counter to 1 and run till greater than or equal to 5. Let's see the codes for it.
#include <stdio.h> int main() { int i, j; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { printf("*"); } printf("\n"); } return 0; }
***** ***** ***** ***** *****
In the above program, we have declared two variables i and j. Variable i is for rows and Variable j is for columns. The for loop having variable i is for changing the rows and the second for loop which is having variable j is for changing the columns and inside that loop, it is printing * everytime. Then once the condition of the second loop is break, then the second statement after loop, is printing the break line. So, once the loop is executed for 5 times then the control goes to new line. And again, the first loop is executed. Like this we can manipulate with the loops and can generate different patterns.
0 Comment