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.
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 function 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) printf ( "%d " , j); else printf ( " " ); } printf ( "\n" ); } } // Driver Code int 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 Code int 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); } } |
Python3
# Python program print hollow half pyramid # pattern using numbers # pattern function def pattern(N): for i in range ( 1 , N + 1 ): for j in range ( 1 , i + 1 ): if j = = 1 or j = = i or i = = N: print (j, end = " " ) else : print ( " " , end = " " ) print () # Driver code if __name__ = = "__main__" : N = 5 pattern(N) |
Javascript
// Function to print a hollow // half pyramid pattern using numbers function printHollowHalfPyramid(N) { // Loop to print numbers from 1 to N for (let i = 1; i <= N; i++) { // Loop to display numbers up to i for (let j = 1; j <= i; j++) { // Print number if it's the first or last one in the row or if it's the last row if (j === 1 || j === i || i === N) { process.stdout.write(`${j} `); // Use process.stdout.write() to print without a newline } else { process.stdout.write( " " ); // Print two spaces for hollow parts } } // Move to a new line after each row is printed process.stdout.write( "\n" ); } } // Main code block let N = 5; // Define the size of the pyramid printHollowHalfPyramid(N); // Call the function to print the pattern |
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 Code int 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 Code int main() { int N = 6; // Function Call pattern(N); return 0; } |
Java
// Java program to print hollow inverted half pyramid // pattern using numbers import java.io.*; // Driver Class class 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); } } |
Python3
# python program to print hollow inverted half pyramid # pattern using numbers #pattern function def pattern(N): for i in range ( 1 , N + 1 ): for j in range (i, N + 1 ): if i = = 1 or j = = i or j = = N: print (j, end = " " ) else : print ( " " , end = " " ) print () # Driver code if __name__ = = "__main__" : N = 6 pattern(N) |
Javascript
// Function to print a hollow inverted // half pyramid pattern using numbers function printHollowInvertedHalfPyramid(N) { // Loop to print numbers from 1 to N for (let i = 1; i <= N; i++) { // Loop to display numbers from i to N for (let j = i; j <= N; j++) { // Print number if it's the first row, or if it's on the diagonal, or if it's on the last column if (i === 1 || j === i || j === N) { process.stdout.write(`${j} `); // Use process.stdout.write() to print without a newline } else { process.stdout.write( " " ); // Print two spaces for hollow parts } } // Move to a new line after each row is printed process.stdout.write( "\n" ); } } // Main code block let N = 6; // Define the size of the pyramid printHollowInvertedHalfPyramid(N); // Call the function to print the pattern |
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.