Here are a few C programs to display some common patterns. These patterns are very fundamental implementation of the loops in programming. These patterns allows you have a better understanding of the looping structures in C. Once you have a clear understanding of these programs, then definitely you will be clear with the for loop of many programming languages.
PATTERN # 1
/******************************************************************************/#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
printf("*");
printf("\n");
}
getch();
return 1;
}
/********************************************************************************/
PATTERN # 2
/********************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
return 1;
}
/********************************************************************************/
PATTERN # 3
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r-i-1;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
printf("*");
printf("\n");
}
getch();
return 1;
}
/********************************************************************************/
PATTERN # 4
/********************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("* ");
printf("\n");
}
getch();
return 1;
}
/********************************************************************************/
PATTERN # 5
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<i;j++)
printf(" ");
for(j=0;j<r-i;j++)
printf("* ");
printf("\n");
}
getch();
return 1;
}
/*******************************************************************************/
PATTERN # 6
/******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("* ");
printf("\n");
}
r--;
for(i=0;i<r;i++)
{
printf(" ");
for(j=0;j<i;j++)
printf(" ");
for(j=0;j<r-i;j++)
printf("* ");
printf("\n");
}
getch();
return 1;
}
/******************************************************************************/
PATTERN # 7
/********************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<i;j++)
printf(" ");
for(j=0;j<r-i;j++)
printf("* ");
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<r-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("* ");
printf("\n");
}
getch();
return 1;
}
/*******************************************************************************/
PATTERN # 8
/********************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r-i-1;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
printf("*");
printf("\n");
}
r--;
for(i=0;i<r;i++)
{
printf(" ");
for(j=0;j<i;j++)
{
printf(" ");
}
for(j=0;j<r-i;j++)
{
printf("*");
}
printf("\n");
}
getch();
return 1;
}
/*******************************************************************************/
PATTERN # 9
/****************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<i;j++)
{
printf(" ");
}
for(j=0;j<r-i;j++)
{
printf("*");
}
printf("\n");
}
getch();
return 1;
}
/*****************************************************************************/
PATTERN # 10
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r-i;j++)
printf("*");
printf("\n");
}
getch();
return 1;
}
/******************************************************************************/
PATTERN # 11
/********************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
r--;
for(i=0;i<r;i++)
{
for(j=0;j<r-i;j++)
printf("*");
printf("\n");
}
getch();
return 1;
}
/********************************************************************************/
PATTERN # 12
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<r-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("* ");
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
printf("* ");
printf("\n");
}
getch();
return 1;
}
/*****************************************************************************/
PATTERN # 13
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,i,j,a=4,p;
clrscr();
printf("ENTER THE NUMBER OF ROWS : \n");
scanf("%d",&r);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
r--;
for(i=0;i<r;i++)
{
for(j=0;j<r-i;j++)
printf("*");
printf("\n");
}
r++;
p=r+1;
for(i=0;i<r;i++)
{
gotoxy(p,a++);
for(j=0;j<r-i-1;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
printf("*");
printf("\n");
}
r--;
p--;
for(i=0;i<r;i++)
{
gotoxy(p,a++);
printf(" ");
for(j=0;j<i;j++)
{
printf(" ");
}
for(j=0;j<r-i;j++)
{
printf("*");
}
}
getch();
return 1;
}
/******************************************************************************/
I'm looking forward for the reader's suggestion and any new pattern you want me to add to this post. Any new and efficient solutions from readers are welcomed.
No comments:
Post a Comment