Open In App

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: 3
Output:
***
**
*



Input: 5
Output:
*****
****
***
**
*

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 an inverted left half pyramid pattern with N rows, the 1st row will have 0 space followed by N stars, the 2nd row will have 1 space followed by (N – 1) stars, the third row will have 2 spaces followed by (N – 2) stars and so on. So, Nth row will have (N – 1) spaces followed by 1 star.

Step-by-step approach:

Below is the implementation of the above approach:




#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 'i - 1' spaces
        for (int j = 1; j <= i - 1; j++) {
            cout << "  ";
        }
           
        // Inner loop prints 'N - i + 1' stars
        for (int j = 1; j <= N - i + 1; j++) {
            cout << "*";
        }
        // Move to the next line
        cout << "\n";
    }
 
    return 0;
}




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 'i - 1' spaces
            for (int j = 1; j <= i - 1; j++) {
                System.out.print("  ");
            }
               
              // Inner loop prints 'N - i + 1' stars
            for (int j = 1; j <= N - i + 1; j++) {
                System.out.print("*");
            }
            // Move to the next line
            System.out.println();
        }
    }
}




# Number of rows
N = 5
 
# Outer loop runs N times, once for each row
for i in range(1, N + 1):
      # Inner loop prints 'i - 1' spaces
    for j in range(1, i):
        print("  ", end="")
         
    # Inner loop prints 'N - i + 1' stars
    for j in range(1, N - i + 2):
        print("*", end="")
    # Move to the next line
    print()




using System;
 
class Program
{
    static void 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 'i - 1' spaces
            for (int j = 1; j <= i - 1; j++)
            {
                Console.Write("  ");
            }
 
            // Inner loop prints 'N - i + 1' stars
            for (int j = 1; j <= N - i + 1; j++)
            {
                Console.Write("*");
            }
 
            // Move to the next line
            Console.WriteLine();
        }
    }
}




// 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 <= i-1; j++) {
        process.stdout.write("  ");
    }
 
    // Inner loop prints 'i' stars
    for (let j = 1; j <= N-i+1; j++) {
        process.stdout.write("* ");
    }
    // Move to the next line
    process.stdout.write("\n");
}

Output
*****
  ****
    ***
      **
        *

Time Complexity: O(N2), where N is the number of rows in the pattern.
Auxiliary Space: O(1)


Article Tags :