C Program to Print the Double Pyramid Pattern
Problem Statement : Program to Print the Double Pyramid Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include<stdio.h> int main() { int i, j, k; int blank = 0; int lines = 6; char symbol = 'A'; int temp; int diff[7] = { 0, 1, 3, 5, 7, 9, 11 }; k = 0; //Step 0 for (i = lines; i >= 0; i--) { printf("\n"); symbol = 'A'; //step 1 for (j = i; j >= 0; j--) { printf("%c\t", symbol++); } //step 2 blank = diff[k++]; //step 3 - Double space for (j = 0; j < blank; j++) { printf("\t"); } symbol = 'F' - (blank / 2); if (blank == 0) { temp = i - 1; } else { temp = i; } for (j = 0; j <= temp; j++) { //step 4 printf("%c\t", symbol--); } } return (0); } |
Output :
1 2 3 4 5 6 7 | 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 |
Explain Me ?
As the above coding is very difficult to understand , so I have divided Program in 5 Steps.Easy steps are as follow ,
Step 0 :
Program has 7 lines i.e (0 to 6)
1 | int i,j,k; |
Variables : i is used to trace the Current Line
Variables : j is used for Subscript variable for Different Loops
Variables : k is used to trace the diff array , for each new value of i (new line) k is incremented
1 | int diff[7]= {0,1,3,5,7,9,11}; |
Variable : diff[7] is used for Storing the number of blanks
eg.
1 | A B C D E F F E D C B A // 2nd Line |
Here G is Missing so blank = 1;
Step 1 :
1 2 3 4 5 6 7 | A B C D E F G A B C D E F A B C D E A B C D A B C A B A |
Will be Printed
Step 2 : Initialize Blank i.e for first line blank = 0 , for second line it will be 1 and so on
1 2 3 4 5 6 7 | A B C D E F G A B C D E F A B C D E A B C D A B C A B A |
Will be Printed, Blanks are denoted by
Step 3 :
1 | symbol = 'F' - (blank/2); |
Here symbol variable is initialized , Consider first Line , We are printing Letter ‘F’ after ‘G’ so initialize it .
for 3rd line
>> blank = 3 , so
>> symbol = ‘F’ - 1 = ‘E’
step 4 :
1 2 | for(j=0;j <= temp;j++) printf("%c ",symbol--); |
Here print remaining characters , until we print ‘A’