9.02.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;
}


/***********************************************************
The output of above program would be:
************************************************************/

Output of character pyramid C program
Figure: 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


You Might Also Like:

  1. Big 98+ C pyramid programs list
  2. Latest user asking pyramid programs list

8 comments:

  1. Sir Please provide me c code for this pattern.
    A B C D E D C B A
    A B C D C B A
    A B C B A
    A B A
    A

    ReplyDelete
    Replies
    1. @pradeep Tiwari

      Your required character pattern C program source code :

      http://cprogrammingcodes.blogspot.com/2015/09/character-pattern-program-in-c.html

      Delete
  2. int printPyramid(char initCh, char finCh)
    {
    //checking whether the given combination is right or wrong
    if(!((finCh >= initCh)&
    (((initCh >=65)&(finCh<=90))|((initCh >=97)&(finCh<=122)))))
    {
    printf("Please insert the correct combination.");
    return 0;
    }

    // Take register storage class so that the processing become fast as register variables operate fastest
    // as coapared to other storage class variables.
    register int m = initCh;
    register int lastInd = ((finCh - initCh)+1)<<1; //left shift (equivalent to multiplication by 2),but more faster than the multiplication.
    int heightOfPyramid = lastInd;

    /* Count down from N to 0 is always faster then counting from 0 to N.
    * Reason as following -
    * Count down from N to 0 -> Load iteration i, compare and jump if it is less then or equal to zero (less instraction at H/W level)
    * Count down from 0 to N -> Load i , load N, sub i and N compare and jump if it is less then or equal to zero (more instraction at H/W level)
    */
    for(lastInd; lastInd>0; lastInd--) // loop over the heigh of the pyramid
    {
    if((lastInd%2)== 0) // print the even string like abccba
    {
    for(m = initCh; m<=finCh; m++) //print in increasing order like abc
    printf("%c", m);
    for(--m; m>=initCh; m--) //print in decreasing order like cba
    printf("%c",m);
    printf("\n");
    }
    else // print the odd string like abcba
    {
    for(m = initCh; m=initCh; --m) //print in decreasing order like cba
    printf("%c",m);
    printf("\n");
    finCh--;
    }
    }
    return heightOfPyramid;
    }

    ReplyDelete
  3. Input an alphabet from the user and display pattern as follows.
    If user press 'G' , output should be as:

    A B C D E F G F E D C B A
    A B C D E F F E D C B A
    A B C D E E D C B A
    A B C D D C B A
    A B C C B A
    A B B A
    A A

    ReplyDelete
    Replies
    1. #Shruti Suryawanshi

      Your required character pattern as above pattern C program source code at:

      http://cprogrammingcodes.blogspot.com/2015/09/character-pattern-program-in-c.html

      Delete
  4. STRING IS INDIA
    OUTPUT SHOULD BE:
    1.)
    I
    .N
    ..D
    ...I
    ....A

    2.)
    A
    II
    DDD
    NNNN
    IIIII

    ReplyDelete
  5. ABCDEF
    BCDEF
    CDEF
    DEF
    EF
    F

    help for this output guys..c program

    ReplyDelete
  6. Whats the pattern for
    INDIAIDNI
    INDI IDNI
    IND DNI
    IN NI
    I I

    ReplyDelete