Problem Statement : Program to Print the Double Pyramid Pattern



#include<stdio.h>
#include<conio.h>
void 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};
 clrscr();
 k=0;
 //--------------------------------Step 0    
  for(i=lines;i>=0;i--)
   {
       printf("\n");
       symbol = 'A';
 //--------------------------------Step 1    
      for(j=i;j>=0;j--)
   printf("%c ",symbol++);
 //--------------------------------Step 2    
 blank = diff[k++];
 for(j=0;j<blank;j++)
  printf("  ");
 //--------------------------------Step 3    
 symbol = 'F' - (blank/2);
 if (blank== 0)
   temp = i-1;
 else
   temp = i;
 //--------------------------------Step 4    
 for(j=0;j<=temp;j++)
   printf("%c ",symbol--);
 }
 getch();
}

Output :
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)
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
int diff[7]= {0,1,3,5,7,9,11};
Variable : diff[7] is used for Storing the number of blanks
eg.
A B C D E F   F E D C B A      // 2nd Line
Here G is Missing so blank = 1;
Step 1 :
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

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 :
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 :
for(j=0;j<=temp;j++)
   printf("%c ",symbol--);
Here print remaining characters , until we print 'A'

Bookmark & Share


Tags / Keywords : | , , ,

3 comments

  1. Archana // May 07, 2025  

    can u help me in finding solution for the below ques.
    to print pyramid pattern

    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

  2. Priteh Taral // May 10, 2025  

    @Archana : Sorry for Delayed Reply ---->

    for(j=0;j<blank;j++)
    printf(" ");

    Comment these two Lines You will get Expected Output

  3. gaurav // September 28, 2024  

    can u help me in finding solution for the question

    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email