In this tutorial we will going to learn alphabet pyramid building in c. In c we can build various pyramids with numbers , alphabets or stars etc. In this tutorial we learn the alphabet based pyramids. Using the alphabets we can implement any type of pyramids some examples are as follow:
program – 1
Implement following alphabet pyramid.
A
BC
DEF
C
123456789101112131415161718192021222324252627282930313233343536373839404142434445 | #include<stdio.h>#include<conio.h>int main(){int i,j,n;char c;clrscr();printf(“Enter number of character till you want pyramid:”);sanf(“%c”,&n);c=’A’;for(i=0;i<n;i++){for(j=0;j<=i;j++){if(c==’Z’)c=’A’;printf(“%c”,c);c++;}printf(“\n”);}getch();return 0;} |
OutPut :
Enter number of character till you want pyramid: 3
A
BC
DEF
In above program, as we are implementing the alphabet pyramid. first we entered the number of row till we want to print alphabets that we taken into the variable n. we require the alphabets A to Z to print that character we taken in c variable. we require iteration for printing for n times we iterated i variable till n and simultaneously we printed the alphabet and incremented the alphabet value so for printing next alphabet. while doing this encounter the end of alphabet so for that again assigned to first alphabet that is A and continue with the program.
program – 2
Implement following alphabet pyramid.
A
AB
ABC
ABCD
ABCDE
ABCDE
ABCD
ABC
AB
A
C
123456789101112131415161718192021222324252627282930313233 | #include<stdio.h>#include<conio.h>void main(){int x,y;static char str[]=”ABCDE”;for(x=0;x<5;x++){y=x+1;printf(“%-5.*s\n,y,str);}for(x=4;x>=0;x–){y=x+1;printf(“%-5.*s\n”,y,str);}getch();} |
OutPut :
A
AB
ABC
ABCD
ABCDE
ABCDE
ABCD
ABC
AB
A
In above program, as we are implementing the pyramid we break the pyramid into the two parts as upper and lower so that task will be easy. In first half we printed the half portion as ABCDE. and after that we printed the last half.
program – 3
Implement following diamond shape alphabet pyramid
A
ABA
ABCBA
ABCDCBA
ABCBA
ABA
A
C
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | #include<stdio.h>#include<conio.h>int main(){char c,r,c;int sp;printf(“Enter any character:”);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);for(c=r-1;c>=’A’;c–)printf(“%c”,c);printf(“\n”);}for(r=’A’ ; A<=ch;ch–, r++){for(sp=r;sp>=’A’;sp–)printf(“ “);for(c=’A’;c<=ch-1;c++)printf(“%c”,c);for(c=ch-2;c>=’A’;c–)printf(“%c”,c);printf(“\n”);}getch();return 0;} |
OutPut :
A
ABA
ABCBA
ABCDCBA
ABCBA
ABA
A