Program for diamond pattern with different layers

3.2

Given a number n and using 0-n numbers u have to print such a pattern.

Examples:

Input : n = 5
Output :
          0
        0 1 0
      0 1 2 1 0
    0 1 2 3 2 1 0
  0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
  0 1 2 3 4 3 2 1 0
    0 1 2 3 2 1 0
      0 1 2 1 0
        0 1 0
          0

Input : n = 3
Output :
      0
    0 1 0
  0 1 2 1 0
0 1 2 3 2 1 0
  0 1 2 1 0
    0 1 0
      0

The idea to count the space in beginning of string. In this pattern there are (2 * n + 1) rows. In rows form 0 to N number of spaces is (2 * n – i). In row number from (n + 1) to (2 * n), number of space is (i – n) * 2.

Below is the c++ code for above approach.

// c++ code to print the pattern
#include <bits/stdc++.h>
using namespace std;

// function to generate the pattern.
void pattern(int n)
{
    // putting the space in line 1
    for (int i = 1; i <= n * 2; i++) 
        cout << " ";    
    cout << 0 << endl;

    // generating the middle pattern.
    for (int i = 1; i <= (n * 2) - 1; i++) {

        // printing the increasing pattern
        if (i < n) {
            for (int j = 1; j <= (n - i) * 2; j++)
                cout << " ";
        }
        else {
            for (int j = 1; j <= (i % n) * 2; j++)
                cout << " ";
        }

        if (i < n) {
            for (int j = 0; j <= i % n; j++)
                cout << j << " ";
            for (int j = (i % n) - 1; j > 0; j--)
                cout << j << " ";
            cout << 0;
        }

        // printing the decreasing pattern
        else if (i > n) {
            for (int j = 0; j <= n - (i - n); j++)
                cout << j << " ";

            for (int j = (n - (i - n)) - 1; j > 0; j--)
                cout << j << " ";
            cout << 0;
        }
        else {
            for (int j = 0; j <= n; j++)
                cout << j << " ";
            for (int j = n - 1; j > 0; j--)
                cout << j << " ";
            cout << 0;
        }
        cout << endl;
    }

    // putting the space in last line
    for (int i = 1; i <= n * 2; i++)
        cout << " ";
    cout << 0;
}

// driver function.
int main()
{
    int n = 4;
    pattern(n);
    return 0;
}

Output:

        0
      0 1 0
    0 1 2 1 0
  0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
  0 1 2 3 2 1 0
    0 1 2 1 0
      0 1 0
        0

This article is contributed by Abhishek Sharma 44. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3.2 Average Difficulty : 3.2/5.0
Based on 7 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.