Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
C++ Program To Print Triangle Pattern
Next article icon

Program to print hollow Triangle pattern

Last Updated : 13 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given the number of lines as N, the task is to form the given hollow Triangle pattern.
Examples: 
 

Input: N = 6  Output:  ************  *****  *****  ****    ****  ***      ***  **        **  *          *

Approach:
 

  1. Input number of rows to print from the user as N.
  2. To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look like for(i=N; i>=1; i–).
  3. To print spaces, run an inner loop from i to space (another local variable). The loop structure should look like for(k=1; k=1; j–); .Inside this loop print star.
  4. After printing all columns of a row, move to next line i.e. print new line.

Below is the implementation of the above approach:
 

C++




// C++ program for printing
// the hollow triangle pattern
  
#include <iostream>
using namespace std;
  
// Function for printing pattern
void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
  
    // For printing stars
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            cout << "*";
        }
        if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
                cout << " ";
            }
  
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) {
            if (j != rows)
                cout << "*";
        }
        cout << "\n";
    }
    cout << "\n";
}
  
// Driver code
int main()
{
  
    // Get N
    int N = 6;
  
    // Print the pattern
    pattern(N);
  
    return 0;
}
 
 

C




// C program for printing
// the hollow triangle pattern
  
#include <stdio.h>
  
// Function for printing pattern
void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
  
    // For printing stars
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
                printf(" ");
            }
  
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) {
            if (j != rows)
                printf("*");
        }
        printf("\n");
    }
    printf("\n");
}
  
// Driver code
int main()
{
  
    // Get N
    int N = 6;
  
    // Print the pattern
    pattern(N);
  
    return 0;
}
 
 

Java




// Java program for printing
// the hollow triangle pattern
import java.util.*;
  
class solution
{
  
// Function for printing pattern
static void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
  
    // For printing stars
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            System.out.print("*");
        }
        if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
            System.out.print(" ");
            }
  
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) {
            if (j != rows)
                System.out.print("*");
        }
        System.out.print("\n");
    }
    System.out.print("\n");
}
  
// Driver code
public static void main(String args[])
{
  
    // Get N
    int N = 6;
  
    // Print the pattern
    pattern(N);
  
} 
}
  
//This code is contributed by Surendra_Gangwar
 
 

Python3




# Python 3 program for printing 
# the hollow triangle pattern 
  
# Function for printing pattern
def pattern(N):
    k, space, rows = 0, 1, N
  
    # For printing stars 
    for i in range(rows, 0, -1):
        for j in range(1, i + 1):
            print('*', end = '')
  
        if i != rows:
            # for printing space 
            for k in range(1, space + 1):
                print(' ', end = '')
  
            # increment by 2 
            space += 2
        for j in range(i, 0, -1):
            if j != rows:
                print('*', end = '')
        print()
    print()
  
# Driver Code
  
# Get N
N = 6
  
# Print the pattern 
pattern(N)
  
# This code is contributed by 
# SamyuktaSHegde 
 
 

C#




// C# program for printing
// the hollow triangle pattern
using System;
  
class GFG
{
  
// Function for printing pattern
static void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
  
    // For printing stars
    for (i = rows; i >= 1; i--) 
    {
        for (j = 1; j <= i; j++) 
        {
            Console.WriteLine("*");
        }
        if (i != rows) 
        {
            // for printing space
            for (k = 1; k <= space; k++)
            {
            Console.Write(" ");
            }
  
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) 
        {
            if (j != rows)
                Console.Write("*");
        }
        Console.Write("\n");
    }
    Console.Write("\n");
}
  
// Driver code
public static void Main()
{
  
    // Get N
    int N = 6;
  
    // Print the pattern
    pattern(N);
} 
}
  
// This code is contributed 
// by Rajput-Ji
 
 

PHP




<?php
// PHP program for printing
// the hollow triangle pattern
  
// Function for printing pattern
function pattern($N)
{
    $k = 0;
    $space = 1;
    $rows = $N;
  
    // For printing stars
    for ($i = $rows; $i >= 1; $i--) 
    {
        for ($j = 1; $j <= $i; $j++) 
        {
            echo "*";
        }
        if ($i != $rows) 
        {
            // for printing space
            for ($k = 1; $k <= $space; $k++)
            {
                echo " ";
            }
  
            // increment by 2
            $space = $space + 2;
        }
        for ($j = $i; $j >= 1; $j--)
        {
            if ($j != $rows)
                echo "*";
        }
        echo "\n";
    }
    echo "\n";
}
  
// Driver code
  
// Get N
$N = 6;
  
// Print the pattern
pattern($N);
  
// This code is contributed by
// Archana_kumari 
?>
 
 

Javascript




<script>
      // JavaScript program for printing
      // the hollow triangle pattern
  
      // Function for printing pattern
      function pattern(N) {
        var i,
          j,
          k = 0,
          space = 1,
          rows = N;
  
        // For printing stars
        for (i = rows; i >= 1; i--) {
          for (j = 1; j <= i; j++) {
            document.write("*");
          }
          if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
              document.write("  ");
            }
  
            // increment by 2
            space = space + 2;
          }
          for (j = i; j >= 1; j--) {
            if (j != rows) document.write("*");
          }
          document.write("<br>");
        }
        document.write("<br>");
      }
  
      // Driver code
      // Get N
      var N = 6;
        
      // Print the pattern
      pattern(N);
        
      // This code is contributed by rdtank.
    </script>
 
 
Output: 
***********  ***** *****  ****   ****  ***     ***  **       **  *         *

 

Time complexity: O(n2)

Auxiliary Space: O(1)



Next Article
C++ Program To Print Triangle Pattern

K

Kanishk_Verma
Improve
Article Tags :
  • C Programs
  • C++ Programs
  • DSA
  • School Programming
  • pattern-printing
Practice Tags :
  • pattern-printing

Similar Reads

  • C++ Program To Print Triangle Pattern
    Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo
    6 min read
  • C Program To Print Floyd Triangle Pattern
    Here, we will build a C Program To Print Floyd Triangle Pattern using 2 approaches i.e. Using for loop Using while loop Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1. Using for loop The first for loop is used to iterate the number of rows and the second for loop is used to repeat the nu
    2 min read
  • C Program to Print Floyd's Triangle Pyramid Patterns
    Floyd’s triangle pyramid pattern is a pattern where numbers or characters are printed in a triangular shape, with each row containing an increasing number of consecutive integers or characters aligned to the left forming a shape similar to right half pyramid pattern. In this article, we will learn h
    2 min read
  • C Program To Print Triangle
    Here, we will see how to print a triangle using the C program Input: 5 Output: * * * * * * * * * * * * * * * Approach: The approach is very simple. We just execute a nested loop, and only print characters when the inner loop is executed after that just change the line. Example: C/C++ Code // C progr
    1 min read
  • C Program To Print Hollow Diamond Pattern
    The Hollow Diamond Pattern is a variation of the diamond pattern where only the edges of the diamond are filled with characters and the inside remains empty. This creates a hollow effect in the shape of a diamond. In this article, we will learn how to print the Hollow Diamond Pattern using C program
    4 min read
  • C Program To Print Hollow Pyramid Patterns
    The Hollow Pyramid patterns are the variation of pyramid patterns where only the outer edges are filled with characters but the interior is left empty. In this article, we will learn how to print different hollow pyramid patterns. There can be 5 hollow pyramid patterns corresponding to each of the n
    12 min read
  • C Program to Print Hollow Hourglass Pattern
    The Hollow Hourglass Pattern is a symmetrical pattern that resembles an hourglass but with hollow spaces inside and only edges having characters. In this article, we will learn how to print the Hollow Hourglass Pattern using a C program. Program to Print Hollow Hourglass Pattern in C[GFGTABS] Star P
    4 min read
  • C++ Program To Print Floyd's Triangle
    Floyd's triangle is a triangular array of natural numbers and it is named after Robert Floyd, a renowned computer scientist popularly known for the design of the Floyd–Warshall algorithm. Here, we will see how to print Floyd's pattern triangle pyramid using the C++ program. Below are the examples: I
    4 min read
  • C Program to Print Hourglass Pattern
    The Hourglass Pattern is a symmetrical pattern similar to an hourglass shape. It can be visualized as an inverted full pyramid placed on a regular full pyramid. In this article, we will learn how to print the Hourglass Pattern using a C program. Program to Print Hourglass Pattern in C[GFGTABS] Star
    4 min read
  • C Program to Print Cross or X Pattern
    The Cross or X Pattern is a pattern where characters or stars are printed diagonally from top-left to bottom-right and from top-right to bottom-left, forming an "X" shape. In this article, we will learn how to print this pattern using a C program. Program to Print Cross or X Pattern[GFGTABS] Star Cr
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences