Open In App

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: 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, the first inner loop will print the spaces and second inner loop will print stars. If we observe carefully, if we have a full 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 3 stars, the third row will have 2 spaces followed by 5 stars and so on. So, Nth row will have 0 spaces followed by (2 * N – 1) stars.

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 'N - i' spaces
        for (int j = 1; j <= N - i; j++) {
            cout << "  ";
        }
           
        // Inner loop prints '2 * i - 1' stars
        for (int j = 1; j <= 2 * 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 'N - i' spaces
            for (int j = 1; j <= N - i; j++) {
                System.out.print("  ");
            }
               
              // Inner loop prints '2 * i - 1' stars
            for (int j = 1; j <= 2 * 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 'N - i' spaces
    for j in range(1, N - i + 1):
        print("  ", end="")
         
    # Inner loop prints '2 * i - 1' stars
    for j in range(1, 2 * i):
        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 'N - i' spaces
            for (int j = 1; j <= N - i; j++)
            {
                Console.Write("  ");
            }
 
            // Inner loop prints '2 * i - 1' stars
            for (int j = 1; j <= 2 * 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 <= N - i; j++) {
        process.stdout.write("  ");
    }
       
    // Inner loop prints '2 * i - 1' stars
    for (let j = 1; j <= 2 * i - 1; j++) {
        process.stdout.write("*");
    }
    // Move to the next line
    console.log();
}
//This code is contributed by Prachi.

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




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


Article Tags :