Q. Print the rectangle pattern program in C as following design:
1111111
1222221
1233321
1234321
1233321
1222221
1111111
Ans.
1111111
1222221
1233321
1234321
1233321
1222221
1111111
/* source code for print the rectangle pattern program*/
#include<stdio.h>
int main()
{
int num=4,r,c,m,n,p,q=6;
for(r=1; r<=num; r++,q=q-2)
{
for(c=1; c<=r; c++)
printf("%d", c);
for(p=q; p>=1; p--)
printf("%d", r);
for(c=r-1; c>=1; c--)
printf("%d", c);
printf("\n");
}
for(r=3,m=1; r>=1; r--,m=m+2)
{
for(c=1; c<=r; c++)
printf("%d", c);
for(n=m; n>=1; n--)
printf("%d", r);
for(c=r; c>=1; c--)
printf("%d", c);
printf("\n");
}
getch();
return 0;
}
You might also like:
1111111
1222221
1233321
1234321
1233321
1222221
1111111
Ans.
1111111
1222221
1233321
1234321
1233321
1222221
1111111
Tips/Tricks: Each color group represent a loop in rectangle pattern program as the below source code written.
/* source code for print the rectangle pattern program*/
#include<stdio.h>
int main()
{
int num=4,r,c,m,n,p,q=6;
for(r=1; r<=num; r++,q=q-2)
{
for(c=1; c<=r; c++)
printf("%d", c);
for(p=q; p>=1; p--)
printf("%d", r);
for(c=r-1; c>=1; c--)
printf("%d", c);
printf("\n");
}
for(r=3,m=1; r>=1; r--,m=m+2)
{
for(c=1; c<=r; c++)
printf("%d", c);
for(n=m; n>=1; n--)
printf("%d", r);
for(c=r; c>=1; c--)
printf("%d", c);
printf("\n");
}
getch();
return 0;
}
/**********************************************************
The output of above program would be:
***********************************************************/
Figure: Screen shot for Print Rectangle Pattern Program In C |
You might also like:
No comments:
Post a Comment