Open In App

C Program To Print Hollow Diamond Pattern

Last Updated : 27 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

To print the hollow diamond pattern in C, we will use the following 2 approaches:

  1. for Loop
  2. while Loop

Input:

n = 5

Output:

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

Approach 1: Using for loop

Example:  

C




// C Program To Print Hollow Diamond
// Pattern using for loop
#include <stdio.h>
int main()
{
 
    int n = 5, rows, columns;
 
    // for loop is used to identify
    // the number of rows and
    // it is used to print upper triangle
    for (rows = 1; rows <= n; rows++) {
 
        // used for printing the spaces
        for (columns = n; columns > rows; columns--) {
            printf(" ");
        }
 
        // print star
        printf("*");
 
        // again print the spaces
        for (columns = 1; columns < (rows - 1) * 2;
             columns++) {
            printf(" ");
        }
        if (rows == 1) {
            printf("\n");
        }
        else {
            printf("*\n");
        }
    }
    // for loop is used to identify
    // the number of rows and
    // it is used to print lower triangle
    for (rows = n - 1; rows >= 1; rows--) {
 
        // used for printing the spaces
        for (columns = n; columns > rows; columns--) {
            printf(" ");
        }
 
        // print star
        printf("*");
        for (columns = 1; columns < (rows - 1) * 2;
             columns++) {
            printf(" ");
        }
        if (rows == 1) {
            printf("\n");
        }
        else {
            printf("*\n");
        }
    }
    return 0;
}


Output

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

Method : Using while loop

Example:

C




// C Program To Print Hollow Diamond
// Pattern using while loop
#include <stdio.h>
 
int main()
{
 
    int n = 5, rows = 1, columns;
    // while loop is used to identify
    // the number of rows and
    // it is used to print upper triangle
    while (rows <= n) {
        columns = n;
 
        // used for printing the spaces
        while (columns > rows) {
            printf(" ");
            columns--;
        }
 
        // print star
        printf("*");
        columns = 1;
        while (columns < (rows - 1) * 2) {
            printf(" ");
            columns++;
        }
        if (rows == 1) {
            printf("\n");
        }
        else {
            printf("*\n");
        }
        rows++;
    }
    // while loop is used to identify
    // the number of rows and
    // it is used to print lower triangle
    rows = n - 1;
    while (rows >= 1) {
        columns = n;
        // used for printing the spaces
        while (columns > rows) {
            printf(" ");
            columns--;
        }
        // print star
        printf("*");
        columns = 1;
        while (columns < (rows - 1) * 2) {
            printf(" ");
            columns++;
        }
        if (rows == 1) {
            printf("\n");
        }
        else {
            printf("*\n");
        }
        rows--;
    }
    return 0;
}


Output

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

Time complexity: O(n2) for given input n
Auxiliary space: O(1) as it is using constant space for variables



Similar Reads

C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern
Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C/C++ Code // C++ program to print hollow diamond pattern #include &lt;iostream&gt; using namespace std; int main(
3 min read
Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
21 min read
Program to print hollow Triangle pattern
Given the number of lines as N, the task is to form the given hollow Triangle pattern.Examples: Input: N = 6 Output: ************ ***** ***** **** **** *** *** ** ** * * Approach: Input number of rows to print from the user as N.To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look l
6 min read
C++ Program To Print Inverted Hollow Star Pyramid Pattern
Given the value of R(number of rows), write a C++ program to print the Inverted Hollow Pyramid using stars and white spaces. Examples: Input: R = 5 Output: ********* * * * * * * * Input: R = 10 Output: ******************* * * * * * * * * * * * * * * * * * Algorithm: At first, take the number of rows as input.The next step is to implement the nested
2 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
C Program To Print Diamond Pattern
Here, we will see how to print a full diamond shape pyramid using the C program. Below are the examples: Input: 6Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Input: 8Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 min read
Program to print hollow rectangle or square star patterns
Hollow rectangle star pattern : The task is print below hollow pattern of given dimension. ******************** * * * * * * * * ******************** Explanation: Input number of rows and columns.For rows of rectangle run the outer loop from 1 to rows.for (i = 1; i &lt; = rows; i++)For column of rectangle run the inner loop from 1 to columns. for (j
4 min read
Program to Print Mirrored Hollow Parallelogram
Given the length as rows and breadth as cols, the task is to print the mirrored hollow parallelogram.Examples: Input: rows = 5, cols = 8 Output: ******** * * * * * * ******** C/C++ Code // CPP program to print hollow mirrored // parallelogram star pattern series #include &lt;iostream&gt; using namespace std; // function for creating pattern void Pa
5 min read
C Program To Print Inverted Hollow Star Pyramid
Here we will see how to print an inverted hollow star pyramid using a C program. Below are the examples: Input: row = 5Output:********* * * * * * * * Input: row = 7Output:************* * * * * * * * * * * * There are 2 ways to print an inverted hollow star pyramid in C: Using For Loop.Using While Loop. Let's start discussing each of these methods i
4 min read
C Program To Print Hollow Star Pyramid
Here, we will print the hollow star pyramid pattern using a C program. Input: n = 5 Output: * * * * * * * *********** C/C++ Code // C Program to Demonstrate // a Hollow Star Pyramid #include &lt;stdio.h&gt; int main() { int i, space, n = 5, j = 0; // first for loop is used to // iterate number of rows for (i = 0; i &lt; n - 1; i++) { // second for
1 min read