Wednesday, 1 February 2012

Character triangle

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


 ABCDEDCBA
   BCDEDCB
      CDEDC
        DED
           E
Ans.
/* c program for character triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
  int sp;
  char ch,r,c;
  printf("Enter pyramid character : ");
  ch=getchar();
  if(ch>='a' && ch<='z')
    ch=ch-32;
  for(r='A'; r<=ch; r++)
  {
     for(sp=r; sp>'A'; sp--)
       printf(" ");
     for(c=r; c<=ch; c++)
       printf("%c",c);
     for(c=ch-1; r<=c; c--)
       printf("%c",c);
     printf("\n");
  }
  return 0;
}

Output:-

Enter pyramid character : e
ABCDEDCBA
 BCDEDCB
  CDEDC
   DED
    E

No comments:

Post a Comment