C program to print diamond pattern

Write a C program to print diamond pattern of stars. This post covers below patterns

 

 


 

Pattern 1: Diamond


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

The problem looks complex at first look but it can be easily solved by breaking it into sub-problems. If we take a closer look, the upper half is almost similar to Pyramid Pattern with n rows and lower half is similar to Inverted Pyramid Pattern with n-1 rows.

C

Download   Run Code

Pattern 2: Hollow Diamond


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

C

Download   Run Code

Pattern 3: Left Half Diamond


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

Like Pattern 1, the problem can be easily divided into sub-problems. The upper half is similar to Right-angled Triangle Pattern with n rows and lower half is similar to Inverted Right-angled Triangle with n-1 rows.

C

Download   Run Code

Pattern 4: Right Half Diamond


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

Similarly, this pattern can be easily printed by breaking it into sub-patterns. The upper half is similar to Right-angled Triangle Pattern with n rows and lower half is similar to Inverted Right-angled Triangle with n-1 rows.

C

Download   Run Code

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz