EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Python for EmSAT Achieve  >  Code: Hollow Diamond Pattern

Code: Hollow Diamond Pattern | Python for EmSAT Achieve PDF Download

1. 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:

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

// CPP program to print a hollow pyramid pattern

#include <iostream>

using namespace std;

void printPattern(int);

int main()

{

    int n = 6;

 

    printPattern(n);

}

void printPattern(int n)

{

    int i, j, k = 0;

    for (i = 1; i <= n; i++) // row=6

    {

        // Print spaces

        for (j = i; j < n; j++) {

            cout << " ";

        }

        // Print #

        while (k != (2 * i - 1)) {

            if (k == 0 || k == 2 * i - 2)

                cout << "#";

            else

                cout << " ";

            k++;

        }

        k = 0;

        cout << endl; // print next row

    }

    // print last row

    for (i = 0; i < 2 * n - 1; i++) {

        cout << "#";

    }

}

Output

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

2. Hollow Diamond

Note: For even input, print the pattern for n-1.

Example:
Input: 1
Output:

       *
For n=1

Input: 7
Output:

  Code: Hollow Diamond Pattern | Python for EmSAT Achieve

Approach: To print diamond we need to print spaces before star and after the star to achieve constant increasing distance of stars.
To print the box shape we need to print ‘-‘ for i==1 (first row) & i==n (last row) and ‘|’ for j==1 (first column) and j==n (last column).

Algorithm: 

  • If n is odd increment n.
  • Find mid=n/2.
  • Traverse from 1 to mid to print upper half of the pattern (say i).
  • Traverse from 1 to mid-i to print spaces for upper left most outer box (say j).
  • If (i==1) print ‘*’ (since for first row we need only one star).
  • else print ‘*’ and traverse from 1 to 2*i-3 to print spaces for hollow diamond (say j) and print ‘*’ after loop is over.
  • Traverse from 1 to mid-i to print spaces again for upper right most outer box (say j).
  • Close the loop at step 3.
  • Traverse from mid+1 to n-1 to print lower half of the pattern (say i).
  • Traverse from 1 to i-mid to print spaces for lower left most outer box (say j).
  • If (i==n-1) print ‘*’ (since for last row we need only one star).
  • else print ‘*’ and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond (say j) and print ‘*’ after loop is over.
  • Traverse from 1 to i-mid to print spaces again for lower right most outer box (say j).
  • Close the loop at step 9.

#include <bits/stdc++.h>

using namespace std;

// function to print the pattern

void printPattern(int& n)

{

    int i,j,mid;

    if(n%2==1) //when n is odd, increase it by 1 to make it even

      n++;

    mid = n/2;

    // upper half pattern

    for(i = 1; i<= mid; i++) {

      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star

         cout<<" ";

      if(i == 1) {

         cout << "*";

      }else{

         cout << "*"; //in each line star at start and end position

         for(j = 1; j<=2*i-3; j++) { //print space to make hollow

            cout << " ";

         }

         cout << "*";

      }

      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star

         cout<<" ";

      cout << endl;

   }

   // lower half pattern

   for(i = mid+1; i<n; i++) {

      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star

         cout<<" ";

      if(i == n-1) {

         cout << "*";

      }else{

         cout << "*"; //in each line star at start and end position

         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow

            cout << " ";

         }

         cout << "*";

      }

      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star

         cout<<" ";

      cout << endl;

   }

}

 // driver's code

int main() {

   int n=7;

   printPattern(n);

}

Output

 Code: Hollow Diamond Pattern | Python for EmSAT Achieve 

3. Hollow Diamond bounded inside a rectangular box made of horizontal and vertical dashes(-).

Write a program to Print hollow diamond pattern bound inside a box made of dash(-) and bitwise-OR(|) as shown below.

Note: For even input, print the pattern for n-1.

Example:

Input: 1
Output:

     *
For n=1

Input: 7
Output:

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

Approach: To print diamond we need to print spaces before star and after the star to achieve constant increasing distance of stars.
To print the box shape we need to print ‘-‘ for i==1 (first row) & i==n (last row) and ‘|’ for j==1 (first column) and j==n (last column).

Algorithm: 

  • If n is odd increment n.
  • Find mid=n/2.
  • Traverse from 1 to mid to print upper half of the pattern (say i).
  • Traverse from 1 to mid-i to print upper left most outer box (say j).
  • If (i==1) print ‘*’ (since for first row we need only one star).
  • else print ‘*’ and traverse from 1 to 2*i-3 to print spaces for hollow diamond (say j) and print ‘*’ after loop is over.
  • Traverse from 1 to mid-i to print upper right most outer box (say j).
  • Close the loop at step 3.
  • Traverse from mid+1 to n-1 to print lower half of the pattern (say i).
  • Traverse from 1 to i-mid to print lower left most outer box (say j).
  • If (i==n-1) print ‘*’ (since for last row we need only one star).
  • else print ‘*’ and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond (say j) and print ‘*’ after loop is over.
  • Traverse from 1 to i-mid to print lower right most outer box (say j).
  • Close the loop at step 9.

#include <bits/stdc++.h>

using namespace std;

// function to print the pattern

void printPattern(int& n)

{

    int i,j,mid;

    if(n%2==1) //when n is odd, increase it by 1 to make it even

      n++;

    mid = n/2;

    // upper half pattern

    for(i = 1; i<= mid; i++) {

      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star

         if(i==1)

         cout<<"-";

         else if(j==1)

         cout << "|";

         else cout<<" ";

      }

      if(i == 1) {

         cout << "*";

      }else{

         cout << "*"; //in each line star at start and end position

         for(j = 1; j<=2*i-3; j++) { //print space to make hollow

            cout << " ";

         }

         cout << "*";

      }

      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star

         if(i==1)

         cout<<"-";

         else if(j==mid-i)

         cout << "|";

         else cout<<" ";

      }

      cout << endl;

   }

   // lower half pattern

   for(i = mid+1; i<n; i++) {

      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star

         if(i==n-1)

         cout<<"-";

         else if(j==1)

         cout << "|";

         else cout<<" ";

      }

      if(i == n-1) {

         cout << "*";

      }else{

         cout << "*"; //in each line star at start and end position

         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow

            cout << " ";

         }

         cout << "*";

      }

      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star

         if(i==n-1)

         cout<<"-";

         else if(j==i-mid)

         cout << "|";

         else cout<<" ";

      }

      cout << endl;

   }

}

// driver's code

int main() {

   int n=12;

   printPattern(n);

}

Output

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

The document Code: Hollow Diamond Pattern | Python for EmSAT Achieve is a part of the EmSAT Achieve Course Python for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
49 videos|38 docs|18 tests

Top Courses for EmSAT Achieve

49 videos|38 docs|18 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

Semester Notes

,

MCQs

,

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

,

Extra Questions

,

Previous Year Questions with Solutions

,

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

,

Sample Paper

,

study material

,

Objective type Questions

,

ppt

,

video lectures

,

Exam

,

Summary

,

Viva Questions

,

Important questions

,

pdf

,

shortcuts and tricks

,

Code: Hollow Diamond Pattern | Python for EmSAT Achieve

,

practice quizzes

,

mock tests for examination

,

Free

;