Monday, 3 December 2012

Character Pyramid Program

Q. Write a C program to print the following character pyramid C program:

ABCD

CDA
AB
C

Ans.

Cross-reference: Before you read answer of above program i recommend to read Number Pyramid Program for easily understand this program. 

/*c program for character pyramid*/
#include<stdio.h>
int main()
{

 char ch='D',n,c,r,t;
 n=ch;
 for(r='A'; r<=ch; r++,n--)
 {
  if(r=='B' || r=='D')
  {
    for(c='A',t='C'; c<=n; c++,t++)
    {
      if(c=='C' && r=='B')
         printf("A");
      else
         printf("%c",t);
    }
  }
  else
  {
    for(c='A'; c<=n; c++)
        printf("%c", c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of character pyramid C program
Figure: Screen shot for character pyramid C program

No comments:

Post a Comment