Showing posts with label character pyramid. Show all posts
Showing posts with label character pyramid. Show all posts

Sunday, 2 September 2012

Character Pyramid

Q. Write a C program for following character triangle.
or
Q. Write a C program for print the following character pyramid.

ABCDEFFEDCBA
ABCDEEDCBA
ABCDDCBA
ABCCBA
ABBA
AA

Ans.

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

/************Output************/

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

Related Programs:

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

123456654321
1234554321
12344321
123321
1221
11


Wednesday, 15 February 2012

Square triangle3

Q. write a C program to display the character in following fashion:
     A
    AB
   ABC
  ABCD
 ABCDE
  ABCD
   ABC
    AB
     A
Ans.



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


/***************** OUTPUT ********************/