OCJP

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();
}
Patter Program C Language (Column Print)

If we replace statement printf(“%d “,j) to printf(“%d “,i) then output will be

Pattern Row Wise In C Language

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();
}
Decreasing Triangle Pattern

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();
}
Reverse Pyramid Pattern C Language

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

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?