While programming, beginners do come up with some form of pyramid to be printed using any kind of loops in most programming languages. This video presentation clarifies the basic concept behind coding these pyramids in any programming language and explains how these loops can be created and manipulated to obtain different outputs.
If you visualize properly, these pyramids are mere TABLES, combination of rows and columns. Having said that, we can understand that there needs to be a logic to control both rows and columns of the table and do that in such a way that the desired pattern is obtained.
For doing so, we use two loops in case of a simple pyramid, one for the rows and other for the columns. Since, the general convention is to represent the rows prior to columns, assuming the same, the outer loop tracks the rows and the inner loop tracks the columns. The outer loop is fairly simple and is defined starting from 0 or 1 depending on your preference in most of the cases and goes upto the number of horizontally aligned rows. What defines the pattern in most cases is the inner loop or specifically the column controlling loop. It needs a condition to satisfy the desired pattern and once you have a clear understanding of the typical condition forming logic, it would get fairly easy for you to go about any kind of similar problem.
Test Code : (Use this code to produce the desired patterns)
For pattern 1 =>
#include<stdio.h>
int main( ) {
for( int i = 0 ; i < 5 ; i++ ) {
for( int j = 0; j < = i; j++ ) {
printf( "%s","*" );
}
printf( "\n" );
}
return 0;
}
For pattern 2 =>
#include<stdio.h>
int main( ) {
for( int i = 0; i < 5; i++ ) {
for( int j = i; j < 5; j++ ) {
printf( "%s","*" );
}
printf( "\n" );
}
return 0;
}
NOTE: Few of the examples of such problems will be discussed soon. Although it is highly recommended to try out few of the examples on your own after you have understood the concept of how it works.
Watch the video below to know more.
If you visualize properly, these pyramids are mere TABLES, combination of rows and columns. Having said that, we can understand that there needs to be a logic to control both rows and columns of the table and do that in such a way that the desired pattern is obtained.
For doing so, we use two loops in case of a simple pyramid, one for the rows and other for the columns. Since, the general convention is to represent the rows prior to columns, assuming the same, the outer loop tracks the rows and the inner loop tracks the columns. The outer loop is fairly simple and is defined starting from 0 or 1 depending on your preference in most of the cases and goes upto the number of horizontally aligned rows. What defines the pattern in most cases is the inner loop or specifically the column controlling loop. It needs a condition to satisfy the desired pattern and once you have a clear understanding of the typical condition forming logic, it would get fairly easy for you to go about any kind of similar problem.
Test Code : (Use this code to produce the desired patterns)
For pattern 1 =>
#include<stdio.h>
int main( ) {
for( int i = 0 ; i < 5 ; i++ ) {
for( int j = 0; j < = i; j++ ) {
printf( "%s","*" );
}
printf( "\n" );
}
return 0;
}
For pattern 2 =>
#include<stdio.h>
int main( ) {
for( int i = 0; i < 5; i++ ) {
for( int j = i; j < 5; j++ ) {
printf( "%s","*" );
}
printf( "\n" );
}
return 0;
}
NOTE: Few of the examples of such problems will be discussed soon. Although it is highly recommended to try out few of the examples on your own after you have understood the concept of how it works.
Watch the video below to know more.
Comments
Post a Comment