Nesting of Loops (Pattern) Programs
As we have discussed earlier regarding nesting of Loops, here by some examples with output that describes more pattern program in C Language.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
If we replace statement printf(“%d “,j) to printf(“%d “,i) then output will be
Now if we change outer loop like for(i=10;i>=1;i–) //Decremental
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=10;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
We have added an extra loop for Increasing Space (one Space in Loop)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=10;i>=1;i--)
{
for(j=10;j>=i;j--)
{
printf(" "); //One Space
}
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
If we replace printf(” “); (Single Space with an Extra Space Two Space) in above program then output will be
If we Interchange Inner Loops like
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=10;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(" "); //Two Space
}
for(j=10;j>=i;j--)
{
printf("* ");
}
printf("\n");
}
getch();
}
Once Again we decrease one space (From Two Space to One Space) then