Print diamond pattern in C and Java

Write a C and Java 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

Java

Download   Run Code

 

Pattern 2: Hollow Diamond


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

C

Download   Run Code

Java

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

Java

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

Java

Download   Run Code

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Leave a Reply

avatar
  Subscribe  
Notify of