Program to Print Left Half Pyramid Pattern (Star Pattern) Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * *****Input: 5Output: * ** *** *********Approach: The problem can be solved using two nested loops inside another loop. The outer loop will run for the rows and the first inner loop will print the spaces and second loop will print stars. If we observe carefully, if we have a left half pyramid pattern with N rows, the 1st row will have 4 spaces followed by 1 star, the 2nd row will have 3 spaces followed by 2 stars, the third row will have 2 spaces followed by 3 stars and so on. So, Nth row will have 0 spaces followed by N stars.Step-by-step approach: Run an outer loop from i = 1 to the number of rows.Run an inner loop from j = 1 to N - i.Print an empty space (' ') in each iteration of the inner loop.Run an inner loop from j = 1 to i.Print an asterisk ('*') in each iteration of the inner loop.Print a newline character ("\n") to move to the next row.After N iterations, we will have the left half pyramid pattern. Below is the implementation of the above approach: C++ #include <iostream> using namespace std; int main() { // Number of rows int N = 5; // Outer loop runs N times, once for each row for (int i = 1; i <= N; i++) { // Inner loop prints 'N - i' spaces for (int j = 1; j <= N - i; j++) { cout << " "; } // Inner loop prints 'i' stars for (int j = 1; j <= i; j++) { cout << "*"; } // Move to the next line cout << "\n"; } return 0; } Java import java.util.Scanner; public class Main { public static void main(String[] args) { // Number of rows int N = 5; // Outer loop runs N times, once for each row for (int i = 1; i <= N; i++) { // Inner loop prints 'N - i' spaces for (int j = 1; j <= N - i; j++) { System.out.print(" "); } // Inner loop prints 'i' stars for (int j = 1; j <= i; j++) { System.out.print("*"); } // Move to the next line System.out.println(); } } } Python # Number of rows N = 5 # Outer loop runs N times, once for each row for i in range(1, N + 1): # Inner loop prints 'N - i' spaces for j in range(1, N - i + 1): print(" ", end="") # Inner loop prints 'i' stars for j in range(1, i + 1): print("*", end="") # Move to the next line print() C# using System; class MainClass { public static void Main(string[] args) { // Number of rows int N = 5; // Outer loop runs N times, once for each row for (int i = 1; i <= N; i++) { // Inner loop prints 'N - i' spaces for (int j = 1; j <= N - i; j++) { Console.Write(" "); } // Inner loop prints 'i' stars for (int j = 1; j <= i; j++) { Console.Write("*"); } // Move to the next line Console.WriteLine(); } } } JavaScript // Number of rows const N = 5; // Outer loop runs N times, once for each row for (let i = 1; i <= N; i++) { // Inner loop prints 'N - i' spaces for (let j = 1; j <= N - i; j++) { process.stdout.write(" "); } // Inner loop prints 'i' stars for (let j = 1; j <= i; j++) { process.stdout.write("*"); } // Move to the next line console.log(); } Output * ** *** **** ***** Time Complexity: O(N^2), where N is the number of rows in the pattern.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to Print Left Half Pyramid Pattern (Star Pattern) M mrityuanjay8vae Follow Improve Article Tags : DSA pattern-printing Practice Tags : pattern-printing Similar Reads Program to Print Inverted Left Half Pyramid Pattern (Star Pattern) Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** 4 min read Program to Print Full Pyramid Pattern (Star Pattern) Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * * 4 min read Program to Print Right Half Pyramid Pattern (Star Pattern) Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: T 3 min read Program to Print Inverted Right Half Pyramid Pattern (Star Pattern) Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Ou 3 min read Program to Print Butterfly Pattern (Star Pattern) Given an integer N, print N rows of Butterfly pattern. Examples: Input: 3Output: * *** ********* *** * Input: 5Output: * *** ***** ******* ***************** ******* ***** *** * Approach: The problem can be solved using three nested loops inside an outer loop. The outer loop will run for the rows, th 6 min read Like