2.15.2012

Square triangle1

Q. write a C program to display the character in following fashion:


       A
      ABA
     ABCBA
    ABCDCBA


Ans.



#include<stdio.h>
#include<conio.h>
int main()
{
 char ch,r,c;
 int sp;
 printf("Enter last character of triangle : ");
 scanf("%c",&ch);
 if(ch>='a' && ch<='z')
   ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
   for(sp=ch-r; sp>=1; sp--)
      printf(" ");
   for(c='A'; c<=r; c++)
      printf("%c",c);
   for(c=r-1; c>='A'; c--)
      printf("%c",c);
   printf("\n");
 }
 getch();
 return 0;
}


/***************** OUTPUT ******************/
Character triangle

1 comment:

  1. Thanks for this! :) Can i ask a favor? Can you help me convert this one into PHP script? I've tried converting it but it displays half a triangle.

    ReplyDelete