Hollow Half Pyramid Pattern Using Numbers
A hollow half-pyramid pattern using numbers is a type of pattern that seems like a pyramid shape mostly it is considered a star pattern but here we will be creating using numbers.

Hollow half-pyramid pattern using numbers
Program to print the following pattern of a half pyramid for N.
Example:
Input: N = 5 1 1 2 1 3 1 4 1 2 3 4 5
Below is the implementation of the above approach:
C
// C program print hollow half pyramid// pattern using numbers#include <stdio.h> // Pattern functionvoid pattern(int N){ int i, j, k = 0; // loop to print number from 1 to N for (i = 1; i <= N; i++) { // For loop to display number upto i for (j = 1; j <= i; j++) { if (j == 1 || j == i || i == N) printf("%d ", j); else printf(" "); } printf("\n"); }} // Driver Codeint main(){ int N = 5; // Function Call pattern(N); return 0;} |
C++
// C++ program print hollow half pyramid// pattern using numbers#include <iostream>using namespace std; void pattern(int N){ int i, j, k = 0; // loop to print number from 1 to N for (i = 1; i <= N; i++) { // For loop to display number upto i for (j = 1; j <= i; j++) { if (j == 1 || j == i || i == N) cout << j << " "; else cout << " "; } cout << endl; }}// Driver Codeint main(){ int N = 5; // Function Call pattern(N); return 0;} |
Java
// Java program print hollow half pyramid // pattern using numbers import java.io.*; class GFG { // Pattern function static void pattern(int N) { int i, j, k = 0; // loop to print number from 1 to N for (i = 1; i <= N; i++) { // For loop to display number upto i for (j = 1; j <= i; j++) { if (j == 1 || j == i || i == N) System.out.print(j+" "); else System.out.print(" "); } System.out.println(); } } // Main Function public static void main(String[] args) { // Variable declared int N=5; // Pattern function called pattern(N); }} |
Output
1 1 2 1 3 1 4 1 2 3 4 5
Time complexity O(N2)
Reason: we are looping through the rows and columns of the pattern.
Space complexity O(1)
Reason: This algorithm does not require any additional space.
Hollow Inverted Half Pyramid Pattern using numbers
Printing an Inverted pyramid of a pattern is mostly the same as printing the pyramid normally just changes in a few conditions and we are all done. Let us check the code.
Example:
input: N = 6 1 2 3 4 5 6 2 6 3 6 4 6 5 6 6
C
// C program to print hollow inverted half pyramid// pattern using numbers#include <stdio.h> void pattern(int N){ int i, j, k = 0; // loop to print number from 1 to N for (i = 1; i <= N; i++) { // For loop to display number from i to N for (j = i; j <= N; j++) { if (i == 1 || j == i || j == N) printf("%d ", j); else printf(" "); } printf("\n"); }} // Driver Codeint main(){ int N = 6; // Function Call pattern(N); return 0;} |
C++
// C++ program to print hollow inverted half pyramid// pattern using numbers#include <iostream>using namespace std; void pattern(int N){ int i, j, k = 0; // loop to print number from 1 to N for (i = 1; i <= N; i++) { // For loop to display number from i to N for (j = i; j <= N; j++) { if (i == 1 || j == i || j == N) cout << j << " "; else cout << " "; } cout << endl; }} // Driver Codeint main(){ int N = 6; // Function Call pattern(N); return 0;} |
Java
// Java program to print hollow inverted half pyramid// pattern using numbersimport java.io.*; // Driver Classclass GFG { static void pattern(int N) { int i, j, k = 0; // loop to print number from 1 to N for (i = 1; i <= N; i++) { // For loop to display number from i to N for (j = i; j <= N; j++) { if (i == 1 || j == i || j == N) System.out.print(j + " "); else System.out.print(" "); } System.out.println(); } } // Main function public static void main(String[] args) { int N = 6; // Pattern function called pattern(N); }} |
Output
1 2 3 4 5 6 2 6 3 6 4 6 5 6 6
Time complexity O(N2)
Reason: we are looping through the rows and columns of the pattern.
Space complexity O(1)
Reason: This algorithm does not require any additional space.


Please Login to comment...