Q. Write a C program to print the following character rectangle structure:
ABCBA
AB BA
A A
AB BA
ABCBA
Ans.
Cross-Reference: Before you reading answer of above program, i recommend to read Number Rectangle Design for easily understand this program.
/*c program for character rectangle pyramid*/
#include<stdio.h>
int main()
{
char ch='C',r,c,q,t,sp,st;
st=ch;
for(r='A'; r<=ch; r++,st--)
{
for(c='A';c<='B'; c++)
{
if(r=='C')
{
printf("A ");
break;
}
else
printf("%c",c);
}
for(sp=r; sp>'A'; sp--)
printf(" ");
for(t=st; t>='A'; t--)
printf("%c",t);
printf("\n");
}
for(r='A'; r<=ch-1; r++)
{
for(c='A'; c<='B'; c++)
printf("%c", c);
for(sp=r; sp<='A'; sp++)
printf(" ");
for(t=r+1; t>='A'; t--)
printf("%c", t);
printf("\n");
}
return 0;
}
The output of above program would be:
ABCBA
AB BA
A A
AB BA
ABCBA
Ans.
Cross-Reference: Before you reading answer of above program, i recommend to read Number Rectangle Design for easily understand this program.
/*c program for character rectangle pyramid*/
#include<stdio.h>
int main()
{
char ch='C',r,c,q,t,sp,st;
st=ch;
for(r='A'; r<=ch; r++,st--)
{
for(c='A';c<='B'; c++)
{
if(r=='C')
{
printf("A ");
break;
}
else
printf("%c",c);
}
for(sp=r; sp>'A'; sp--)
printf(" ");
for(t=st; t>='A'; t--)
printf("%c",t);
printf("\n");
}
for(r='A'; r<=ch-1; r++)
{
for(c='A'; c<='B'; c++)
printf("%c", c);
for(sp=r; sp<='A'; sp++)
printf(" ");
for(t=r+1; t>='A'; t--)
printf("%c", t);
printf("\n");
}
return 0;
}
The output of above program would be:
Figure: Screen shot for character rectangle structure C program |
No comments:
Post a Comment